3

我正在尝试使用WinHttp函数获取对用户信息的请求。此处引用了REST APISugarSync调用。

我的代码如下:

DWORD dwBytesWritten = 0;
DWORD dwStatusCode = 0;
DWORD dwSize = sizeof(DWORD);
BOOL  bResults = FALSE;
string data;
HINTERNET hSession = NULL, 
hConnect = NULL,
hRequest = NULL;

WINHTTP_PROXY_INFO proxyInfo;
winhttpintObj.setProxyInfo(proxyInfo);

HINTERNET hSession = WinHttpOpen(  L"A WinHTTP Example Program/1.0", 
                                   WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
                                   WINHTTP_NO_PROXY_NAME, 
                                   WINHTTP_NO_PROXY_BYPASS, 0);

HINTERNET hConnect = WinHttpConnect( hSession, L"api.sugarsync.com",   
                                     INTERNET_DEFAULT_HTTPS_PORT, 0);

HINTERNET hRequest = WinHttpOpenRequest( hConnect, 
                                         L"GET", L"/user/<userID>", 
                                         NULL, 
                                         WINHTTP_NO_REFERER, 
                                         0, 
                                         WINHTTP_FLAG_SECURE);

string header("Authorization: ");
header += (accessToken);
header += "\n";
wstring ws = wstring(header.begin(), header.end());
WinHttpAddRequestHeaders( hRequest, ws.c_str(), (ULONG)-1L, WINHTTP_ADDREQ_FLAG_ADD );

WinHttpSendRequest( hRequest,
                    WINHTTP_NO_ADDITIONAL_HEADERS,
                    0,
                    WINHTTP_NO_REQUEST_DATA,
                    0, 
                    0, 
                    0 );

WinHttpReceiveResponse( hRequest, NULL);

WinHttpQueryHeaders( hRequest, 
                     WINHTTP_QUERY_STATUS_CODE |
                     WINHTTP_QUERY_FLAG_NUMBER,
                     NULL, 
                     &dwStatusCode, 
                     &dwSize, 
                     NULL );

cout << "dwStatusCode: " << dwStatusCode << endl;

accessTokenhttps://api.sugarsync.com/authorization/......。_ 返回的状态码是401: Authorization required。但是,我已经在标题中指定了它。我可以知道问题发生在哪里以及解决问题的可能方法是什么?谢谢你。

4

0 回答 0