winhttp
// winhttp.cpp : 定义控制台应用程序的入口点。//
#include "stdafx.h"
#include <string>
#include <iOStream>
#include <windows.h>
#include <winhttp.h>
#pragma comment(lib,"winhttp.lib")
#pragma comment(lib,"user32.lib")
BOOL HttpSend(wchar_t* pwszType, wchar_t* pwszIp, wchar_t* pwcsSourcePath, char* pszOutData, int nOutDataLen)
{
Dword dwSize = 0;
wchar_t* lpHeadBuffer = NULL;
HINTERNET hsession = NULL;
HINTERNET hConnect = NULL;
HINTERNET hrequest = NULL;
BOOL bResults = FALSE;
BOOL bUTF8Code = TRUE;
hSession=WinHttpOpen(L"User Agent",WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,WINHTTP_NO_PROXY_NAME,WINHTTP_NO_PROXY_BYPASS,0);
if(hSession)
{
hConnect=WinHttpConnect(hSession, pwszIp,INTERNET_DEFAULT_HTTP_PORT,0);
}
if(hConnect)
{
hRequest=WinHttpOpenRequest(hConnect, pwszType, pwcsSourcePath,L"HTTP/1.1", WINHTTP_NO_referer,WINHTTP_DEFAULT_ACCEPT_types,0);
}
if(hRequest)
{
bResults=WinHttpSendRequest(hRequest,WINHTTP_NO_ADDITIONAL_headers, 0,WINHTTP_NO_REQUEST_DATA, 0, 0, 0 );
}
if(bResults)
{
bResults=WinHttpReceiveresponse(hRequest,NULL);
}
if(bResults)
{
bResults=WinHttpQueryHeaders(hRequest,WINHTTP_QUERY_RAW_HEADERS_CRLF, WINHTTP_HEADER_NAME_BY_INDEX,NULL,&dwSize,WINHTTP_NO_HEADER_INDEX);
if( GetLastERROR() == ERROR_INSUFFICIENT_BUFFER)
{
lpHeadBuffer = new wchar_t[dwSize/sizeof(wchar_t)];
bResults = WinHttpQueryHeaders(hRequest,WINHTTP_QUERY_RAW_HEADERS_CRLF,WINHTTP_HEADER_NAME_BY_INDEX, lpHeadBuffer, &dwSize,WINHTTP_NO_HEADER_INDEX);
}
if ( NULL != wcsstr(lpHeadBuffer, L"charset=gbk") )
{
bUTF8Code = FALSE;
}
}
delete [] lpHeadBuffer;
BOOL bFirstRead = TRUE;
LPVOID lpOutBuffer = NULL;
LPSTR pszOutBuffer = NULL;
dword dwDownloaded = 0;
int nSize = 0;
if(bResults)
{
do
{
// Check for available data.
dwSize = 0;
if ( !WinHttpQueryDataAvailable( hRequest, &dwSize) )
{
break;
}
if ( TRUE == bFirstRead && 0 == dwSize )
{
bResults = FALSE;
break;
}
else if ( 0 == dwSize )
{
break;
}
bFirstRead = FALSE;
pszOutBuffer = new char[dwSize+1];
if (!pszOutBuffer)
{
bResults = FALSE;
break;
}
zeromemory(pszOutBuffer, dwSize+1);
if ( !WinHttpReadData(hRequest,(LPVOID)pszOutBuffer, dwSize, &dwDownloaded) || (0 == dwDownloaded) )
{
delete [] pszOutBuffer;
break;
}
if ( FALSE == bUTF8Code )
{
// 转成UTF-8
int len = MultiByteToWideChar(CP_ACP, 0, pszOutBuffer, -1, NULL, 0);
wchar_t* pwszunicode = new wchar_t[len];
memset(pwszUnicode, 0, sizeof(pwszUnicode));
MultiByteToWideChar(CP_ACP, 0, pszOutBuffer, -1, pwszUnicode, len);
int len2 = WideCharToMultiByte(CP_UTF8, 0, pwszUnicode, len, NULL, 0, NULL, NULL);
nSize += len2;
if ( nOutDataLen < nSize )
{
bResults = FALSE;
break;
}
WideCharToMultiByte(CP_UTF8, 0, pwszUnicode,len, pszOutData+strlen(pszOutData)
, nOutDataLen-strlen(pszOutData), NULL, NULL);
}
else
{
if ( nOutDataLen < dwSize )
{
bResults = FALSE;
break;
}
strcpy_s(pszOutData, nOutDataLen, pszOutBuffer);
}
delete [] pszOutBuffer;
} while (dwSize > 0);
}
if (hRequest) WinHttpClosehandle(hRequest);
if (hConnect) WinHttpCloseHandle(hConnect);
if (hSession) WinHttpCloseHandle(hSession);
return bResults;
}
int _tmain(int argc, _TCHAR* argv[])
{
BOOL bRet = FALSE;
char szOutData[0x1000] = {0};
bRet = HttpSend(L"GET", L"ip.taobao.com", L"/service/getIpInfo.php?ip=124.207.113.2", szOutData, sizeof(szOutData));
if ( !bRet )
{
return 0;
}
memset(szOutData, 0, sizeof(szOutData));
bRet = HttpSend(L"POST", L"pv.sohu.com", L"/cityjson", szOutData, sizeof(szOutData));
system("pause");
return 0;
}
参考网址:http://www.cnblogs.com/ytjjyy/archive/2012/05/05/2484844.html
相关阅读
参考博客:https://blog.csdn.net/thanklife/article/details/70909661WinHTTP提供以下功能:WinHttpAddRequestHeaders 向HTTP请求句
由于项目升级,在数据传输过程中需要经过OAuth2.0认证,访问服务器需要https协议。 首先,实现C++代码访问https 服务器,实现G
WinHttp用法(WinHttp.WinHttpRequest.5.1方法,属性)
先保存下来: 使用WinHttpRequest伪造HTTP头信息,伪造Referer等信息 由于微软封锁了XmlHttp对象,所以无法伪造部分HTTP头信息,但是Win