111,129
社区成员
发帖
与我相关
我的任务
分享
void UpdateData()
{
HINTERNET hSession;
HINTERNET hConnect;
HINTERNET hRequest;
BOOL httpResult;
DWORD data;
DWORD dwSize = sizeof(DWORD);
LPWSTR pwszURL;
DWORD dwStatusCode = 0;
DWORD dwTemp = 0;
CString szText=TEXT("www.google.com");
// Use WinHttpOpen to obtain a session handle.
hSession = WinHttpOpen( L"HTTP Send Request Program/1.0", WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
WINHTTP_NO_PROXY_NAME,
WINHTTP_NO_PROXY_BYPASS, 0);
if (!hSession)
return FALSE;
// Use WinHttpQueryOption to retrieve Internet options.
httpResult = WinHttpQueryOption( hSession, WINHTTP_OPTION_CONNECT_TIMEOUT, &data, &dwSize);
if (!httpResult)
return FALSE;
hConnect = WinHttpConnect( hSession, L"www.google.com", INTERNET_DEFAULT_HTTP_PORT, 0);
if (!hConnect)
goto END;
// Open and Send a Request Header.
hRequest = WinHttpOpenRequest( hConnect, L"GET",
szText,
NULL, WINHTTP_NO_REFERER, WINHTTP_DEFAULT_ACCEPT_TYPES, 0);
if (!hRequest)
goto END;
httpResult = WinHttpSendRequest( hRequest, WINHTTP_NO_ADDITIONAL_HEADERS, 0, WINHTTP_NO_REQUEST_DATA, 0, 0, 0);
if (!hRequest)
goto END;
httpResult = WinHttpReceiveResponse(hRequest, NULL);
if (!hRequest)
goto END;
// Check the status code.
httpResult = WinHttpQueryHeaders( hRequest,
WINHTTP_QUERY_STATUS_CODE|
WINHTTP_QUERY_FLAG_NUMBER, NULL,
&dwStatusCode, &dwSize, NULL);
if (httpResult)
{
switch (dwStatusCode)
{
case 200:
// The resource was successfully retrieved.
// You could use WinHttpReadData to read the contents of the server's response.
break;
default:
// The status code does not indicate success.
//printf("\nThe server responded with a status code of %i.", dwStatusCode);
// error
goto END;
break;
}
}
END:
// When finished, release the hRequest handle.
httpResult = WinHttpCloseHandle(hRequest);
if (!httpResult)
return FALSE;
// When finished, release the hConnect handle.
httpResult = WinHttpCloseHandle(hConnect);
if (!httpResult)
return FALSE;
// When finished, release the hSession handle.
httpResult = WinHttpCloseHandle(hSession);
if (!httpResult)
return FALSE;
return TRUE;
}