///
// Windows Internet Function
//
// 1. InternetAttemptConnect(0);
// 2. InternetOpen();
// 3. InternetConnect();
// 4. HttpOpenRequest() / HttpOpenRequestEx() / HttpAddRequestHeaders();
// 5. HttpSendRequest() / HttpSendRequestEx();
// 6. HttpQueryInfo();
// 7. InternetReadFile() / InternetWriteFile();
// 8. InternetCloseHandle();
// OR
// 1. InternetAttemptConnect(0);
// 2. InternetOpen();
// 3. InternetOpenUrl();
// 4. HttpQueryInfo();
// 5. InternetReadFile() / InternetWriteFile();
// 6. InternetCloseHandle();
#ifndef __WININET_UTIL_H__
#define __WININET_UTIL_H__
#include <stdio.h>
#include <stdlib.h>
#include <Windows.h>
#include <WinInet.h>
#include <atldef.h>
#include <atlbase.h>
#include <assert.h>
#pragma comment(lib, "wininet.lib")
//szUrl = _T("http://218.206.177.44:8180/Z3GFilm/clientGate")
DWORD WinInet_ConnectServer(LPCTSTR szUrl, TCHAR *szHeaders, HINTERNET *pHConnect)
{
assert(NULL != szHeaders );
assert(NULL != pHConnect);
BOOL bRet = FALSE;
DWORD dwRet = -1;
if (ERROR_SUCCESS != ::InternetAttemptConnect(0))
return FALSE;
if (!::InternetCheckConnection(szUrl, FLAG_ICC_FORCE_CONNECTION, 0))
return FALSE;
HINTERNET hInternet = NULL;
HINTERNET hConnect = NULL;
hInternet = ::InternetOpen(NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
if (NULL != hInternet)
{
hConnect = ::InternetOpenUrl(hInternet, szUrl, szHeaders, -1,
INTERNET_FLAG_DONT_CACHE | INTERNET_FLAG_KEEP_CONNECTION |
INTERNET_FLAG_RELOAD | INTERNET_FLAG_PRAGMA_NOCACHE, 0);
if (NULL != hConnect)
{
*pHConnect = hConnect;
dwRet = 0;
}
}
return dwRet;
}
DWORD WinInet_GetStatusCode(HINTERNET hConnect, DWORD *pdwStatusCode)
{
DWORD dwRet = -1;
// 查询 HTTP 状态
DWORD dwValue, dwNumberOfBytesRead = sizeof(DWORD);
if (::HttpQueryInfo(hConnect, HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER, &dwValue, &dwNumberOfBytesRead, NULL))
{
*pdwStatusCode = dwValue;
dwRet = 0;
}
return dwRet;
}
// 查询数据大小
DWORD WinInet_GetContentLen(HINTERNET hConnect, DWORD *pdwContentLen)
{
DWORD dwRet = -1;
DWORD dwValue, dwNumberOfBytesRead = sizeof(DWORD);
if (::HttpQueryInfo(hConnect, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &dwValue, &dwNumberOfBytesRead, NULL))
{
*pdwContentLen = dwValue;
dwRet = 0;
}
return dwRet;
}
DWORD WinInet_GetResponseHeaders(HINTERNET hConnect, TCHAR *szHeaders, DWORD *pdwHeadersLen)
{
assert(NULL != hConnect);
assert(NULL != szHeaders);
assert(NULL != pdwHeadersLen);
BOOL fResult = FALSE;
DWORD dwHeadersLen = 0;
fResult = ::HttpQueryInfo(hConnect, HTTP_QUERY_RAW_HEADERS_CRLF, NULL, &dwHeadersLen, 0);
if (*pdwHeadersLen <= dwHeadersLen)
{
*pdwHeadersLen = dwHeadersLen+1;
return -1;
}
fResult = ::HttpQueryInfo(hConnect, HTTP_QUERY_RAW_HEADERS_CRLF, szHeaders, &dwHeadersLen, 0);
*pdwHeadersLen = dwHeadersLen;
return dwHeadersLen;
}
DWORD WinInet_GetResponseBody(HINTERNET hConnect, BYTE *szBody, DWORD *pdwBodyLen)
{
assert(NULL != hConnect);
assert(NULL != szBody);
assert(NULL != pdwBodyLen);
BOOL bResult = FALSE;
DWORD dwBytesAvailable = 0;
DWORD dwBytesTotalRead = 0;
DWORD dwBytesRead = 0;
while (InternetQueryDataAvailable(hConnect, &dwBytesAvailable, 0, 0))
{
if (*pdwBodyLen <= dwBytesAvailable +1)
{
//the length of buffer to recv is too small
*pdwBodyLen = dwBytesAvailable +1;
return -1;
}
bResult = InternetReadFile(hConnect, szBody+dwBytesTotalRead,dwBytesAvailable, &dwBytesRead);
if (!bResult)
{
break;
}
if (dwBytesRead == 0)
break; // End of File.
dwBytesTotalRead += dwBytesRead;
}
szBody[dwBytesTotalRead] = '\0';
*pdwBodyLen = dwBytesTotalRead;
return dwBytesTotalRead;
//const int nSize = 8129;
//BYTE pbBuffer[nSize];
//do
//{
// if (!::InternetReadFile(hConnect, &pbBuffer, nSize, &dwNumberOfBytesRead))
// __leave;
// if (dwNumberOfBytesRead != 0)
// {
// // do something ... ...
// pbBuffer[dwNumberOfBytesRead] = '\0';
// }
//}
//while (dwNumberOfBytesRead != 0);
}
DWORD WinInet_CloseHandles(HINTERNET hInternet, HINSTANCE hConnect)
{
if (NULL != hConnect)
{
::InternetCloseHandle(hConnect);
}
if ( NULL !=hInternet)
{
::InternetCloseHandle(hInternet);
}
return 0;
}
void WinInet_DemoTestFunc()
{
DWORD dwErr = -1;
BOOL bRet = FALSE;
HINTERNET hOpen = ::InternetOpen(NULL, INTERNET_OPEN_TYPE_PRECONFIG, NULL, NULL, 0);
HINTERNET hConnectHandle = ::InternetConnect(hOpen, _T("192.168.10.154"), 8180, NULL, NULL, INTERNET_SERVICE_HTTP, 0, 0);
dwErr = ::GetLastError();//返回0
DWORD dwFlag = INTERNET_FLAG_KEEP_CONNECTION | INTERNET_FLAG_NO_AUTO_REDIRECT | INTERNET_FLAG_NO_CACHE_WRITE | INTERNET_FLAG_NO_COOKIES;
HINTERNET hOpenRequestHandle = ::HttpOpenRequest(hConnectHandle, NULL, NULL, NULL, NULL, NULL, dwFlag, 0);
TCHAR szHeaders[] = _T("message: unusedticketselectrequest\r\nContent-Length: 0\r\nToken: MIIBxTCCAVswgZACAQEwMxMgU0lQOjEzNDAwMDAxMTExQEJKLmhvbWUuY21jYy5jb20TCzEzNDA4MDAzMDQyEwATAAoBAQQgNjQ3MDkxMjI2NjgxNDcwMTQzMDQzOTkxOTI5Mjk3OTgwIhMPMjAxMTEwMjEwODMyMTNaEw8yMDIyMDEyNjA4MzIxM1owBgoBAAoBADAFAgEABAAwgcUEEDY2NnlmcTBvMzgxcWc3dTIEgbCf4fY9nzaFQlexY/RJHhDmZZQZi5kzmrsBlyvMHDnd3ctWfZK7+gKJt2FnpuAI3Ra3PDGg1mCZt8c2X+gYt0kvv7kP846tEe6G1oRPnNLhwEcy22ZKAiHbBVANQuNBVdRUjYjrV7fq5Y0JYvBhnf3u8tiGvmTuiCcZjnRPK01kD13qJopI3ERhVI2pJSHj2Ajey3oyOleDpDQTkckoEemBHWJUKodW6JCWaMO1XW/0VgoBAQoBABMWYmouYXNmLmNoaW5hbW9iaWxlLmNvbRMvTmpZMmVXWnhNRzh6T0RGeFp6ZDFNZz09QGJqLmFzZi5jaGluYW1vYmlsZS5jb20wFQoBAAQQSLt0uEbIQh1DRZj0nLRQ9A==");
LPCTSTR lpHeader = _T("GET /img/baidu_sylogo1.gif /HTTP/1.1")_T("Referer: http://www.baidu.com/");
DWORD dwLen = _tcslen(lpHeader);
bRet = ::HttpAddRequestHeaders(hOpenRequestHandle, lpHeader, dwLen, HTTP_ADDREQ_FLAG_REPLACE|HTTP_ADDREQ_FLAG_ADD);//返回1
bRet = ::HttpSendRequest(hOpenRequestHandle, NULL, 0, NULL, 0);//返回1
TCHAR szBuffer[2048];
DWORD dwNumberOfBytesRead = _countof(szBuffer);
bRet = ::HttpQueryInfo(hConnectHandle, HTTP_QUERY_RAW_HEADERS_CRLF, szBuffer, &dwNumberOfBytesRead, 0);
::InternetCloseHandle(hOpenRequestHandle);
::InternetCloseHandle(hConnectHandle);
::InternetCloseHandle(hOpen);
}
#endif
WinInet api 的使用
最新推荐文章于 2020-01-02 00:17:48 发布