HTTP HTTPS POST GET(包含curl版本和winhttp兩種實現)
阿新 • • 發佈:2019-01-11
玩過抓包,網路協議分析的朋友肯定都知道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方法。
看上去還不錯吧!下面講講使用方法。
一:關於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,不使用預編譯頭。
部署完成,使用如下
1、拷貝WinHttpClient資料夾到專案程式碼目錄。
2、直接使用。
今天帶給大家的是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,不使用預編譯頭。
部署完成,使用如下
二:winhttp實現的WinHttpClient使用較為簡單,如下#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);
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