HInternet只工作一次

时间:2015-06-06 21:31:11

标签: c++ wcf

此代码位于游戏中注入的C ++ DLL中。 (这是在私人服务器上,所以它是合法的)。代码必须多次执行。但是,它只是第一次工作。它可能在WCF端,但我设法从之前从c ++ dll加载的.net dll发送2和5个成功请求。

由于我没有弄清楚为什么在2次和5次请求后它被阻止,我决定全部使用本机,但现在我在一次请求后被阻止了。我觉得它现在与我解析响应的方式有关。

第一次执行获得200个代码状态,第二次执行获得0。

编辑:我的防火墙已关闭。

HINTERNET hInternet = InternetOpen(_T("MyApp"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);

HINTERNET hConnect = InternetConnect(hInternet, _T("localhost"), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);

LPCSTR rgpszAcceptTypes[] = { _T("application/json"), NULL };

HINTERNET hRequest = HttpOpenRequest(hConnect, _T("POST"), _T("/xxxBackend/service1.svc/SaveDataVoid"), NULL, NULL, rgpszAcceptTypes, 0, 0);

HttpAddRequestHeaders(hRequest, _T("Content-Type: application/json\r\n"), -1, HTTP_ADDREQ_FLAG_ADD);

char *JsonData = "{\"data\":{\"AccountName\":\"\",\"CharName\":\"SilverDeth-IV\",\"GameDiff\":\"1\",\"CompressedData\":[8228138568842941382,8247906688399250381,8244242016143283142]}}";
HttpSendRequest(hRequest, NULL, 0, JsonData, strlen(JsonData));

DWORD StatusCode = 0;
DWORD StatusCodeLen = sizeof(StatusCode);
HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &StatusCode, &StatusCodeLen, NULL);

if (StatusCode == 200)
{
    char *lpBuffer[2000];
    DWORD lpdwNumberOfBytesRead = 0;

    InternetQueryDataAvailable(hRequest, &StatusCodeLen, 0, 0);
    bool bRetval = InternetReadFile(hRequest, lpBuffer, 2000, &lpdwNumberOfBytesRead);


}

InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hInternet);

1 个答案:

答案 0 :(得分:0)

您显示的代码根本没有进行任何错误处理。添加它,然后查看失败的确实位置。 StatusCode为0的事实意味着HttpQueryInfo()失败,如果您没有获得有效的hRequest,则可能会发生这种情况。

BTW,rgpszAcceptTypes需要声明为LPCTSTR而不是LPCSTR,您应该使用TEXT()宏而不是_T()宏。 TEXT()属于Win32 API,但_T()属于C运行时库,您未在此代码中使用。

试试这个:

void InetError(LPCTSTR msg)
{
    DWORD dwErr = GetLastError();
    if (dwErr == ERROR_INTERNET_EXTENDED_ERROR)
    {
        DWORD dwInetErr = 0;
        LPTSTR szResp = NULL;
        DWORD dwLength = 0;

        InternetGetLastResponseInfo(&dwInetErr, NULL, &dwLength);
        if (GetLastError() == ERROR_INSUFFICIENT_BUFFER)
        {
            szResp = new TCHAR[dwLength+1];
            InternetGetLastResponseInfo(&dwInetErr, szResp, &dwLength);
        }

        // use msg, szResp, and dwInetErr as needed...
        delete[] szResp;
    }
    else
    {
        // use msg and dwErr as needed...
    }
}

...

HINTERNET hInternet = NULL;
HINTERNET hConnect = NULL;
HINTERNET hRequest = NULL;

hInternet = InternetOpen(TEXT("MyApp"), INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (!hInternet)
{
    InetError(TEXT("InternetOpen failed"));
    goto done;
}

hConnect = InternetConnect(hInternet, TEXT("localhost"), INTERNET_DEFAULT_HTTP_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
if (!hConnect)
{
    InetError(TEXT("InternetConnect failed"));
    goto done;
}

LPCTSTR rgpszAcceptTypes[] = { TEXT("application/json"), NULL };

hRequest = HttpOpenRequest(hConnect, TEXT("POST"), TEXT("/xxxBackend/service1.svc/SaveDataVoid"), NULL, NULL, rgpszAcceptTypes, 0, 0);
if (!hRequest)
{
    InetError(TEXT("HttpOpenRequest failed"));
    goto done;
}

if (!HttpAddRequestHeaders(hRequest, TEXT("Content-Type: application/json\r\n"), -1, HTTP_ADDREQ_FLAG_ADD))
{
    InetError(TEXT("HttpAddRequestHeaders failed"));
    goto done;
}

char *JsonData = "{\"data\":{\"AccountName\":\"\",\"CharName\":\"SilverDeth-IV\",\"GameDiff\":\"1\",\"CompressedData\":[8228138568842941382,8247906688399250381,8244242016143283142]}}";
if (!HttpSendRequest(hRequest, NULL, 0, JsonData, strlen(JsonData)))
{
    InetError(TEXT("HttpSendRequest failed"));
    goto done;
}

DWORD StatusCode = 0;
DWORD StatusCodeLen = sizeof(StatusCode);
if (!HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &StatusCode, &StatusCodeLen, NULL))
{
    InetError(TEXT("HttpQueryInfo failed"));
    goto done;
}

if (StatusCode == 200)
{
    BYTE Buffer[2000];
DWORD dwNumberOfBytes = 0;
DWORD dwNumberOfBytesRead = 0;

if (!InternetQueryDataAvailable(hRequest, &dwNumberOfBytes, 0, 0))
    {
        InetError(TEXT("InternetQueryDataAvailable failed"));
        goto done;
    }

if (!InternetReadFile(hRequest, Buffer, min(dwNumberOfBytes, sizeof(Buffer)), &dwNumberOfBytesRead))
    {
        InetError(TEXT("InternetReadFile failed"));
        goto done;
    }

    //...
}

done:
if (hRequest) InternetCloseHandle(hRequest);
if (hConnect) InternetCloseHandle(hConnect);
if (hInternet) InternetCloseHandle(hInternet);