winhttp是windows网络库,要测试自己写的post请求是否有效,首先得在postman上面建立一个可用的接口。我的如下。
代码思路如下:
1、首先使用WinHttpCrackUrl拆解链接,后面会使用到拆解出来的信息。
2、再使用WinHttpOpen初始化
3、WinHttpSetTimeouts设置超时,这一步可有可无。
4、WinHttpConnect链接到目标,这里就需要使用拆解出来的信息了
5、WinHttpOpenRequest创建一个http请求
6、WinHttpAddRequestHeaders这里添加请求头,需要注意的是请求头类型需要和postman中的头相对应。
7、WinHttpSendRequest发送请求
8、WinHttpReceiveResponse收取回答
9、WinHttpQueryDataAvailable、WinHttpReadData查询读取回复
整个winhttp的post请求就是这么简单
注:const wchar_t *wzUrl 表示链接地址
std::string &cstrRecv 表示获取到的回复
DWORD dwMaxSize回复的最大长度
std::string data表示发送参数

bool PostWebData ( const wchar_t *wzUrl , std::string &cstrRecv , DWORD dwMaxSize, std::string data)
{
cstrRecv.clear();
HINTERNET hSession = NULL , hConnect = NULL , hRequest = NULL;
PVOID pTempBuf = NULL;
DWORD dwDownloaded = 0 , dwTotalDownloaded = 0;
DWORD dwSize = 0;
DWORD dwTotal = 0;
DWORD dwFlag = 0;
URL_COMPONENTS urlComp = {0};
urlComp.dwStructSize = sizeof(urlComp);
urlComp.dwSchemeLength = -1;
urlComp.dwHostNameLength = -1;
urlComp.dwUrlPathLength = -1;
urlComp.dwExtraInfoLength = -1;
std::string sendData = data;
const void* ss = (const char*)sendData.c_str();
if ( !WinHttpCrackUrl(wzUrl , wcslen(wzUrl) , 0 , &urlComp) )
return false;
hSession = WinHttpOpen ( L"User-Agent" ,
WINHTTP_ACCESS_TYPE_DEFAULT_PROXY ,
WINHTTP_NO_PROXY_NAME ,
WINHTTP_NO_PROXY_BYPASS ,
0 );
if ( hSession == NULL )
{
return false;
}
WinHttpSetTimeouts( hSession, 20000 , 20000 , 0 , 0 );
std::wstring wstrHostName = urlComp.lpszHostName;
wstrHostName = wstrHostName.substr ( 0 , urlComp.dwHostNameLength );
hConnect = WinHttpConnect ( hSession , wstrHostName.c_str() , urlComp.nPort , 0 );
if ( hConnect == NULL )
{
goto Failed1;
}
if ( urlComp.nPort == INTERNET_DEFAULT_HTTPS_PORT )
{
dwFlag = WINHTTP_FLAG_SECURE;
}
else
{
dwFlag = 0;
}
hRequest = WinHttpOpenRequest ( hConnect ,
L"POST" ,
urlComp.lpszUrlPath ,
L"HTTP/1.1" ,
WINHTTP_NO_REFERER ,
WINHTTP_DEFAULT_ACCEPT_TYPES ,
dwFlag );
if ( hRequest == NULL )
{
goto Failed2;
}
LPCWSTR header = L"Content-type: application/x-www-form-urlencoded\r\n";
SIZE_T len = lstrlenW(header);
WinHttpAddRequestHeaders(hRequest,header,DWORD(len), WINHTTP_ADDREQ_FLAG_ADD);
if(!hRequest)
goto Failed3;
if ( !WinHttpSendRequest(hRequest, 0, 0,const_cast<void *>(ss),sendData.length(), sendData.length(), 0 ) )
{
goto Failed3;
}
if ( !WinHttpReceiveResponse(hRequest , NULL) )
{
goto Failed3;
}
do
{
dwSize = 0;
if ( !WinHttpQueryDataAvailable(hRequest, &dwSize) )
{
goto Failed3;
}
if ( dwSize == 0 )
break;
dwTotal += dwSize;
if ( dwTotal >= dwMaxSize )
{
goto Failed3;
}
pTempBuf = calloc ( 1 , dwSize + 16 );
if ( pTempBuf == NULL )
{
goto Failed3;
}
if ( !WinHttpReadData(hRequest , (PVOID)((UINT64)pTempBuf) , dwSize , &dwDownloaded) )
{
free ( pTempBuf );
goto Failed3;
}
if ( dwDownloaded == 0 )
{
free ( pTempBuf );
break;
}
cstrRecv += (char*)pTempBuf;
free ( pTempBuf );
dwTotalDownloaded += dwDownloaded;
} while (dwSize > 0);
WinHttpCloseHandle ( hRequest );
WinHttpCloseHandle ( hConnect );
WinHttpCloseHandle ( hSession );
return true;
Failed3:
WinHttpCloseHandle ( hRequest );
Failed2:
WinHttpCloseHandle ( hConnect );
Failed1:
WinHttpCloseHandle ( hSession );
return false;
}
















