php调用winhttp,HTTP HTTPS POST GET(包含curl版本和winhttp两种实现)

玩过抓包,网络协议分析的朋友肯定都知道http https post get,web端和用户的交互主要是通过post get完成的。

今天带给大家的是C++版本的http https get post,只会易语言的朋友请移步。

我这里有两种实现:

1:libcurl实现的CHttpClient类,该类实现了Htpp和Https的get post方法。

2:winhttp实现的WinHttpClient类,同样也实现了Htpp和Https的get post方法。

两者使用起来都很方便灵活。

先上Demo调用代码和效果图。使用方法和源代码随后。

// curlDemo.cpp : 定义控制台应用程序的入口点。

//

#include "stdafx.h"

#include

#include "WinHttpClient/WinHttpClient.h"

#include "httpclient.h"

using namespace std;

wstring UTF8ToUnicode( const string &str )

{

int len = 0;

len = str.length();

int unicodeLen = ::MultiByteToWideChar( CP_UTF8,

0,

str.c_str(),

-1,

NULL,

0 );

wchar_t *pUnicode;

pUnicode = new wchar_t[unicodeLen + 1];

memset(pUnicode, 0, (unicodeLen + 1)*sizeof(wchar_t));

::MultiByteToWideChar( CP_UTF8,

0,

str.c_str(),

-1,

(LPWSTR)pUnicode,

unicodeLen );

wstring rt;

rt = ( wchar_t * )pUnicode;

delete pUnicode;

return rt;

}

int _tmain(int argc, _TCHAR* argv[])

{

string strResponse;

//curl CHttpClient Test

CHttpClient client;

client.Get("http://www.baidu.com",strResponse);

MessageBoxW(NULL,UTF8ToUnicode(strResponse).c_str(),L"http://www.baidu.com",MB_OK);

strResponse.clear();

client.Gets("https://www.alipay.com",strResponse);

MessageBoxW(NULL,UTF8ToUnicode(strResponse).c_str(),L"https://www.alipay.com",MB_OK);

strResponse.clear();

client.Get("http://so.baiduyun.me/search.php?wd=google",strResponse);

MessageBoxW(NULL,UTF8ToUnicode(strResponse).c_str(),L"http://so.baiduyun.me/search.php?wd=google",MB_OK);

strResponse.clear();

client.Post("http://so.baiduyun.me/search.php","wd=google",strResponse);

MessageBoxW(NULL,UTF8ToUnicode(strResponse).c_str(),L"http://so.baiduyun.me/search.php?wd=google",MB_OK);

//winhttp WinHttpClient Test

WinHttpClient WinClient(L"https://itunes.apple.com/cn/lookup?id=527563481");

WinClient.SetRequireValidSslCertificates(false);

WinClient.SendHttpRequest(L"GET");

wstring httpResponseContent = WinClient.GetResponseContent();

MessageBoxW(NULL,httpResponseContent.c_str(),L"http://www.baidu.com",MB_OK);

return 0;

}

运行部分效果图:

0818b9ca8b590ca3270a3433284dd417.png 

0818b9ca8b590ca3270a3433284dd417.png 

0818b9ca8b590ca3270a3433284dd417.png 

看上去还不错吧!下面讲讲使用方法。

一:关于libcurl方式实现的CHttpClient注意事项。

1、ibcurl编译为静态库,代码生成选项,Debug版本请使用MTd,Release请使用MT。

0818b9ca8b590ca3270a3433284dd417.png 

2、预处理器请添加CURL_STATICLIB;

0818b9ca8b590ca3270a3433284dd417.png 

使用方法:

1、将curl文件夹和httpclient.h,httpclient.cpp拷贝到项目代码目录。

2、将项目目录下的httpclient.h,httpclient.cpp添加到项目中。

3、选中httpclient.cpp,不使用预编译头。

0818b9ca8b590ca3270a3433284dd417.png 

部署完成,使用如下

#include "httpclient.h"

string strResponse;

//curl CHttpClient Test

CHttpClient client;

client.Get("http://www.baidu.com",strResponse);

MessageBoxW(NULL,UTF8ToUnicode(strResponse).c_str(),L"http://www.baidu.com",MB_OK);

二:winhttp实现的WinHttpClient使用较为简单,如下

1、拷贝WinHttpClient文件夹到项目代码目录。

2、直接使用。

#include "WinHttpClient/WinHttpClient.h"

//winhttp WinHttpClient Test

WinHttpClient WinClient(L"https://itunes.apple.com/cn/lookup?id=527563481");

WinClient.SetRequireValidSslCertificates(false);

WinClient.SendHttpRequest(L"GET");

wstring httpResponseContent = WinClient.GetResponseContent();

MessageBoxW(NULL,httpResponseContent.c_str(),L"http://www.baidu.com",MB_OK);

完整Demo下载地址(VS2010项目):

http://download.csdn.net/detail/sunflover454/9170719

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值