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

来源:互联网 发布:c语言sleep没反应 编辑:程序博客网 时间:2024/05/16 17:44
玩过抓包,网络协议分析的朋友肯定都知道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 <iostream>#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;}
运行部分效果图:
 
 
 
看上去还不错吧!下面讲讲使用方法。

一:关于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,不使用预编译头。


 
部署完成,使用如下
#include "httpclient.h" string strResponse;//curl CHttpClient TestCHttpClient 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 TestWinHttpClient 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

1 0