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

转自:http://blog.csdn.net/sunflover454/article/details/49030803

玩过抓包,网络协议分析的朋友肯定都知道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调用代码和效果图。使用方法和源代码随后。

[cpp]  view plain  copy
  1. // curlDemo.cpp : 定义控制台应用程序的入口点。  
  2. //  
  3.    
  4. #include "stdafx.h"  
  5. #include <iostream>  
  6. #include "WinHttpClient/WinHttpClient.h"  
  7. #include "httpclient.h"  
  8. using namespace std;  
  9.    
  10. wstring UTF8ToUnicode( const string &str )  
  11. {  
  12.         int  len = 0;  
  13.         len = str.length();  
  14.         int  unicodeLen = ::MultiByteToWideChar( CP_UTF8,  
  15.                 0,  
  16.                 str.c_str(),  
  17.                 -1,  
  18.                 NULL,  
  19.                 0 );  
  20.         wchar_t   *pUnicode;  
  21.         pUnicode = new  wchar_t[unicodeLen + 1];  
  22.         memset(pUnicode, 0, (unicodeLen + 1)*sizeof(wchar_t));  
  23.         ::MultiByteToWideChar( CP_UTF8,  
  24.                 0,  
  25.                 str.c_str(),  
  26.                 -1,  
  27.                 (LPWSTR)pUnicode,  
  28.                 unicodeLen );  
  29.         wstring  rt;  
  30.         rt = ( wchar_t * )pUnicode;  
  31.         delete  pUnicode;  
  32.    
  33.         return  rt;  
  34. }  
  35.    
  36. int _tmain(int argc, _TCHAR* argv[])  
  37. {  
  38.         string strResponse;  
  39.         //curl CHttpClient Test  
  40.         CHttpClient client;  
  41.         client.Get("http://www.baidu.com",strResponse);  
  42.         MessageBoxW(NULL,UTF8ToUnicode(strResponse).c_str(),L"http://www.baidu.com",MB_OK);  
  43.         strResponse.clear();  
  44.         client.Gets("https://www.alipay.com",strResponse);  
  45.         MessageBoxW(NULL,UTF8ToUnicode(strResponse).c_str(),L"https://www.alipay.com",MB_OK);  
  46.         strResponse.clear();  
  47.          client.Get("http://so.baiduyun.me/search.php?wd=google",strResponse);  
  48.         MessageBoxW(NULL,UTF8ToUnicode(strResponse).c_str(),L"http://so.baiduyun.me/search.php?wd=google",MB_OK);  
  49.         strResponse.clear();  
  50.         client.Post("http://so.baiduyun.me/search.php","wd=google",strResponse);  
  51.         MessageBoxW(NULL,UTF8ToUnicode(strResponse).c_str(),L"http://so.baiduyun.me/search.php?wd=google",MB_OK);  
  52.    
  53.         //winhttp WinHttpClient Test  
  54.         WinHttpClient WinClient(L"https://itunes.apple.com/cn/lookup?id=527563481");  
  55.         WinClient.SetRequireValidSslCertificates(false);  
  56.         WinClient.SendHttpRequest(L"GET");  
  57.         wstring httpResponseContent = WinClient.GetResponseContent();  
  58.         MessageBoxW(NULL,httpResponseContent.c_str(),L"http://www.baidu.com",MB_OK);  
  59.    
  60.         return 0;  
  61. }  
运行部分效果图:
 
 
 
看上去还不错吧!下面讲讲使用方法。

一:关于libcurl方式实现的CHttpClient注意事项。
1、ibcurl编译为静态库,代码生成选项,Debug版本请使用MTd,Release请使用MT。

 

2、预处理器请添加CURL_STATICLIB;

 

使用方法:
1、将curl文件夹和httpclient.h,httpclient.cpp拷贝到项目代码目录。
2、将项目目录下的httpclient.h,httpclient.cpp添加到项目中。
3、选中httpclient.cpp,不使用预编译头。


 
部署完成,使用如下
[cpp]  view plain  copy
  1. #include "httpclient.h"  
  2.    
  3. string strResponse;  
  4. //curl CHttpClient Test  
  5. CHttpClient client;  
  6. client.Get("http://www.baidu.com",strResponse);  
  7. MessageBoxW(NULL,UTF8ToUnicode(strResponse).c_str(),L"http://www.baidu.com",MB_OK);  


二:winhttp实现的WinHttpClient使用较为简单,如下
1、拷贝WinHttpClient文件夹到项目代码目录。
2、直接使用。

[cpp]  view plain  copy
  1. #include "WinHttpClient/WinHttpClient.h"  
  2.    
  3. //winhttp WinHttpClient Test  
  4. WinHttpClient WinClient(L"https://itunes.apple.com/cn/lookup?id=527563481");  
  5. WinClient.SetRequireValidSslCertificates(false);  
  6. WinClient.SendHttpRequest(L"GET");  
  7. wstring httpResponseContent = WinClient.GetResponseContent();  
  8. MessageBoxW(NULL,httpResponseContent.c_str(),L"http://www.baidu.com",MB_OK);  

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

http://download.csdn.net/detail/sunflover454/9170719
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值