WinHttp的简单使用用例

原创 2018年04月17日 18:14:00
// 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

WinHTTP中的身份认证

WinHTTP中的身份认证 Microsoft Windows HTTP Services (WinHTTP) fully support the client side use of the Mi...
  • edger2heaven
  • edger2heaven
  • 2015-05-11 14:43:54
  • 1436

WinHTTP中的SSL

WinHTTP中的SSLMicrosoft Windows HTTP服务(WinHTTP)支持安全套接字层(SSL)事务,包括客户端证书。 本主题说明SSL事务中涉及的概念以及如何使用WinHTTP处...
  • ktpd_pro
  • ktpd_pro
  • 2017-03-06 09:25:48
  • 706

使用WinHttp接口编程的心得

最近尝试用
  • sunnyleevip
  • sunnyleevip
  • 2014-11-06 15:07:23
  • 1191

WinInet与WinHttp总结

  • 2014年11月12日 14:21
  • 61KB
  • 下载

配置 WinHTTP 的代理设置

配置 WinHTTP 的代理设置 适用于: Exchange Server 2010 SP2, Exchange Server 2010 SP3 在 MicrosoftExchan...
  • t1993ing
  • t1993ing
  • 2016-12-08 13:50:08
  • 5747

WinHTTP会话概述

WinHTTP会话概述Microsoft Windows HTTP服务(WinHTTP)公开了一组C / C ++函数,使您的应用程序能够访问Web上的HTTP资源。 本主题概述了如何使用这些函数与H...
  • ktpd_pro
  • ktpd_pro
  • 2017-03-03 10:49:36
  • 326

使用WinHttp接口实现HTTP协议Get、Post和文件上传功能

本文讲述如何使用WinHttp接口完成Http Post Get Upload请求。
  • breaksoftware
  • breaksoftware
  • 2014-04-14 21:16:30
  • 45077

WinHTTP WebSocket 代码

  • 2013年05月08日 20:44
  • 35KB
  • 下载

在WinHTTP中使用SSL

在WinHTTP中使用SSL Microsoft Windows HTTP Services (WinHTTP) supports Secure Sockets Layer (SSL) tran...
  • edger2heaven
  • edger2heaven
  • 2015-05-12 07:35:36
  • 3537
收藏助手
不良信息举报
您举报文章:WinHttp的简单使用用例
举报原因:
原因补充:

(最多只允许输入30个字)