必威体育Betway必威体育官网
当前位置:首页 > IT技术

WinHttp的简单使用用例

时间:2019-10-19 17:18:04来源:IT技术作者:seo实验室小编阅读:59次「手机版」
 

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

相关阅读

WinHTTP功能函数

参考博客:https://blog.csdn.net/thanklife/article/details/70909661WinHTTP提供以下功能:WinHttpAddRequestHeaders 向HTTP请求句

C++用winhttp实现https访问服务器

   由于项目升级,在数据传输过程中需要经过OAuth2.0认证,访问服务器需要https协议。    首先,实现C++代码访问https 服务器,实现G

WinHttp用法(WinHttp.WinHttpRequest.5.1方法,属性)

先保存下来: 使用WinHttpRequest伪造HTTP头信息,伪造Referer等信息 由于微软封锁了XmlHttp对象,所以无法伪造部分HTTP头信息,但是Win

分享到:

栏目导航

推荐阅读

热门阅读