从GitHub获取JSON时,InternetReadFile返回奇怪的字符

时间:2018-07-11 20:56:05

标签: c++ json github get wininet

当调用InternetReadFile()从GitHub API读取包含我的要旨数据的JSON数据时遇到问题。我遇到了一堆像à!Ì¿›这样的奇怪字符。我非常确定这不是令牌错误,因为我的令牌可以在我编写的Javascript中使用。

HINTERNET hOpen = InternetOpen(L"Github name", INTERNET_OPEN_TYPE_DIRECT, NULL, NULL, 0);
if (!hOpen)
{
    Log::Error("failed to openinternet\nCode: %ld", GetLastError());
    return false;
}

HINTERNET hConnect = InternetConnect(hOpen, L"api.github.com", INTERNET_DEFAULT_HTTPS_PORT, NULL, NULL, INTERNET_SERVICE_HTTP, NULL, NULL);
if (!hConnect)
{
    Log::Error("failed to connect\nCode: %ld", GetLastError());
    return false;
}

BOOL bEnableDecoding = TRUE;
if (!InternetSetOption(hConnect, INTERNET_OPTION_HTTP_DECODING, &bEnableDecoding, sizeof(bEnableDecoding)))
{
    Log::Error("setoptions failed\nCode: %ld", GetLastError());
    return false;
}

LPCWSTR lplpszTypes[2] = { L"*/*", NULL };
HINTERNET hRequest = HttpOpenRequest(hConnect, L"GET", L"/gists/gist id", NULL, NULL, lplpszTypes, INTERNET_FLAG_SECURE, 0);
if (!hRequest)
{
    Log::Error(" creating request\nCode: %ld", GetLastError());
    return false;
}

if (!HttpSendRequest(hRequest, L"Authorization: token MYTOKEN\r\n", 0, NULL, 0))
{
    Log::Error(" httpsendrequest\nCode: %ld", GetLastError());
    return false;
}

char responseText[256];
int statusCode;
DWORD responseTextSize = sizeof(responseText);
if (!HttpQueryInfo(hRequest, HTTP_QUERY_STATUS_TEXT, &responseText, &responseTextSize, NULL))
{
    statusCode = atoi(responseText);
    Log::Error("status code error\nCode: %ld", GetLastError());
    return false;
}
statusCode = atoi(responseText);

char szData[2048];
DWORD dwRead = 0;
while (InternetReadFile(hRequest, szData, 2048, &dwRead) && dwRead)
{
    jsonoutput->append(szData, dwRead);
}

InternetCloseHandle(hRequest);
InternetCloseHandle(hConnect);
InternetCloseHandle(hOpen);
return true;

0 个答案:

没有答案