16,473
社区成员
发帖
与我相关
我的任务
分享
#include <windows.h>
#include <stdio.h>
#include <string>
using namespace std;
#include <winhttp.h>
#pragma comment(lib, "winhttp.lib")
#include <Wincrypt.h>
#pragma comment(lib, "Crypt32.lib")
WCHAR g_sAgentName[] = L"ZC Firm";
INTERNET_PORT m_wPort = INTERNET_DEFAULT_HTTPS_PORT;
// *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
HINTERNET g_hSession = NULL;
HINTERNET g_hConnect = NULL;
HINTERNET g_hRequest = NULL;
int g_secureFlags = WINHTTP_FLAG_REFRESH | WINHTTP_FLAG_SECURE;//WINHTTP_FLAG_ESCAPE_PERCENT |
// *** *** ***
void zcCrackUrl(WCHAR* _url);
URL_COMPONENTS g_uc = {0};
const DWORD BUF_LEN = 256;
WCHAR g_ucHost[BUF_LEN] = {0};
WCHAR g_ucPath[BUF_LEN] = {0};
char g_bufReadData[1024] = {0};
void ClearHandles();
void CALLBACK zcCallBack(
IN HINTERNET _hInternet,
IN DWORD_PTR _dwContext,
IN DWORD _dwInternetStatus,
IN LPVOID _lpvStatusInformation OPTIONAL,
IN DWORD _dwStatusInformationLength);
void zcTest()
{
ClearHandles(); // 关闭3个 HINTERNET 句柄
zcCrackUrl(L"https://passport.jd.com/new/login.aspx");
g_hSession = WinHttpOpen(g_sAgentName, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY, NULL, NULL, WINHTTP_FLAG_ASYNC);
if (! g_hSession)
{
printf("Cannot open internet : %d\n", GetLastError());
return;
}
g_hConnect = WinHttpConnect(
g_hSession,
g_ucHost,
m_wPort,
0);
if (! g_hConnect)
{
printf("Cannot connect to internet : %d\n", GetLastError());
ClearHandles();
return;
}
g_hRequest = WinHttpOpenRequest(
g_hConnect,
L"HEAD",
g_ucPath,
NULL,
L"",
NULL,
g_secureFlags);
if (! g_hRequest)
{
printf("Cannot perform http request : %d\n", GetLastError());
ClearHandles();
return;
}
//*
// *** 不保持连接状态(每次请求完HEAD都断开)
DWORD dwOption = WINHTTP_DISABLE_KEEP_ALIVE;
int result = WinHttpSetOption(
g_hRequest,
WINHTTP_OPTION_DISABLE_FEATURE,
&dwOption,
sizeof(DWORD));
if (! result)
{
printf("WinHttpSetOption(3) err : %d\n", GetLastError());
return;
}
//*/
if (WINHTTP_INVALID_STATUS_CALLBACK ==
WinHttpSetStatusCallback(g_hRequest, zcCallBack, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 0))
{
printf("WinHttpSetStatusCallback(2) err : %d\n", ::GetLastError());
return;
}
// *** *** *** Send ***
result = WinHttpSendRequest(
g_hRequest,
WINHTTP_NO_ADDITIONAL_HEADERS,
0,
WINHTTP_NO_REQUEST_DATA,
0,
0,
33);
if (! result)
{
printf("WinHttpSendRequest err : %d\n", GetLastError());
return;
}
}
DWORD g_dwCnt = 0;
void main()
{
printf("before zcTest\n");
system("PAUSE");
while (1)
{
g_dwCnt ++;
for (int i=1; i<50000; i++)
{
zcTest();
printf("zcTest out %d (%u)\n", i, g_dwCnt);
Sleep(500);
}
}
}
void ClearHandles()
{
BOOL bRtn = false;
if (g_hRequest)
{
if (WINHTTP_INVALID_STATUS_CALLBACK == WinHttpSetStatusCallback(g_hRequest, NULL, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 0))
{ printf("WinHttpSetStatusCallback(3) err : %d\n", ::GetLastError()); }
bRtn = WinHttpCloseHandle(g_hRequest);
if (! bRtn)
{ printf("WinHttpCloseHandle g_hRequest - err : %d\n", ::GetLastError()); }
g_hRequest = NULL;
}
if (g_hConnect)
{
if (WINHTTP_INVALID_STATUS_CALLBACK == WinHttpSetStatusCallback(g_hConnect, NULL, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 0))
{ printf("WinHttpSetStatusCallback(4) err : %d\n", ::GetLastError()); }
bRtn = WinHttpCloseHandle(g_hConnect);
if (! bRtn)
{ printf("WinHttpCloseHandle g_hConnect - err : %d\n", ::GetLastError()); }
g_hConnect = NULL;
}
if (g_hSession)
{
if (WINHTTP_INVALID_STATUS_CALLBACK == WinHttpSetStatusCallback(g_hSession, NULL, WINHTTP_CALLBACK_FLAG_ALL_NOTIFICATIONS, 0))
{ printf("WinHttpSetStatusCallback(5) err : %d\n", ::GetLastError()); }
bRtn = WinHttpCloseHandle(g_hSession);
if (! bRtn)
{ printf("WinHttpCloseHandle g_hSession - err : %d\n", ::GetLastError()); }
g_hSession = NULL;
}
}
void zcCrackUrl(WCHAR* _url)
{
g_uc.dwStructSize = sizeof(g_uc);
g_uc.lpszHostName = g_ucHost;
g_uc.dwHostNameLength = BUF_LEN;
g_uc.lpszUrlPath = g_ucPath;
g_uc.dwUrlPathLength = BUF_LEN;
WCHAR extra[BUF_LEN];
g_uc.lpszExtraInfo = extra;
g_uc.dwExtraInfoLength = BUF_LEN;
if (!WinHttpCrackUrl(_url, 0, ICU_ESCAPE, &g_uc))
{
printf("WinHttpCrackUrl err : %d\n\n", ::GetLastError());
}
}
void CALLBACK zcCallBack(
IN HINTERNET _hInternet,
IN DWORD_PTR _dwContext,
IN DWORD _dwInternetStatus,
IN LPVOID _lpvStatusInformation OPTIONAL,
IN DWORD _dwStatusInformationLength)
{
if (_dwContext == 33)
{
if (_dwInternetStatus == WINHTTP_CALLBACK_STATUS_SENDREQUEST_COMPLETE)
{
int result = WinHttpReceiveResponse(g_hRequest, 0);
if (! result)
{
return;
}
}
else if (_dwInternetStatus == WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE)
{
char bufHeaders[1024] = {0};
DWORD dwSizeHeaders = 1024;
int result = WinHttpQueryHeaders(
g_hRequest,
WINHTTP_QUERY_STATUS_CODE,
WINHTTP_HEADER_NAME_BY_INDEX,
bufHeaders,
&dwSizeHeaders,
WINHTTP_NO_HEADER_INDEX);
if (result)
{
printf("WINHTTP_CALLBACK_STATUS_HEADERS_AVAILABLE(%d) : %S\n", dwSizeHeaders, bufHeaders);
}
}
else if (_dwInternetStatus == WINHTTP_CALLBACK_STATUS_READ_COMPLETE)
{
printf("WINHTTP_CALLBACK_STATUS_READ_COMPLETE(%d) : \n", _dwStatusInformationLength);
}
}
}
线程 'Win32 线程' (0xe38) 已退出,返回值为 0 (0x0)。
线程 'Win32 线程' (0xfd4) 已退出,返回值为 0 (0x0)。
线程 'Win32 线程' (0x274) 已退出,返回值为 0 (0x0)。
线程 'Win32 线程' (0x900) 已退出,返回值为 0 (0x0)。
线程 'Win32 线程' (0x940) 已退出,返回值为 12017 (0x2ef1)。
线程 'Win32 线程' (0x514) 已退出,返回值为 0 (0x0)。
线程 'Win32 线程' (0xd28) 已退出,返回值为 0 (0x0)。
线程 'Win32 线程' (0xbfc) 已退出,返回值为 12017 (0x2ef1)。
线程 'Win32 线程' (0xf64) 已退出,返回值为 0 (0x0)。
线程 'Win32 线程' (0x6d0) 已退出,返回值为 997 (0x3e5)。
线程 'Win32 线程' (0xd00) 已退出,返回值为 0 (0x0)。
线程 'Win32 线程' (0x82c) 已退出,返回值为 0 (0x0)。
在运行时,有些线程不能正常退出。。不知道啥原因。。
返回值为0,代表正常退出。