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

阅读更多
个人分类: C++
相关热词: winhttp库
上一篇一个UTF-8 与Unicode的转换网站
下一篇CreateThread与_beginthreadex本质区别
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭