VS2015编译Poco+openssl,使用Poco发送HTTPS请求
下載源碼、安裝Openssl
下載Poco源碼
openssl下載安裝:
下載地址:http://slproweb.com/products/Win32OpenSSL.html
Light沒有靜態庫及頭文件,不要使用
openssl最好安裝在c盤根目錄,稍后要在poco編譯文件中設置路徑
編譯POCO
使用VS2015,故修改build_vs140.cmd
修改build_vs140.cmd
@echo OFF
if defined VS140COMNTOOLS (
call "%VS140COMNTOOLS%\vsvars32.bat")
buildwin 140 build shared both Win32 samples tests
不修改應該也可以,多個vs版本時需要指定,下面是命令行幫助:
rem buildwin VS_VERSION [ACTION] [LINKMODE] [CONFIGURATION] [PLATFORM] [SAMPLES] [TESTS] [TOOL] [ENV] [VERBOSITY [LOGGER] ]
rem VS_VERSION: ? ?140|150|160
rem ACTION: ? ? ? ?build|rebuild|clean
rem LINKMODE: ? ? ?static_mt|static_md|shared|all
rem CONFIGURATION: release|debug|both
rem PLATFORM: ? ? ?Win32|x64
rem SAMPLES: ? ? ? samples|nosamples
rem TESTS: ? ? ? ? tests|notests
rem TOOL: ? ? ? ? ?devenv|vcexpress|wdexpress|msbuild
rem ENV: ? ? ? ? ? env|noenv (active only with msbuild, defaulted to env)
rem VERBOSITY ? ? ?quiet|minimal|normal|detailed|diagnostic
rem LOGGER ? ? ? ? <logger path> see msbuild /?
rem
rem VS_VERSION is required argument. Default is build all.
修改builidwin.cmd
在文件中添加openssl環境路徑,根據自己的安裝路徑進行修改:
set OPENSSL_DIR=C:\OpenSSL-Win32
set OPENSSL_INCLUDE=%OPENSSL_DIR%\include;%OPENSSL_DIR%\include\openssl
set OPENSSL_LIB=%OPENSSL_DIR%\lib;%OPENSSL_DIR%\lib\VC
set INCLUDE=%INCLUDE%;%OPENSSL_INCLUDE%
set LIB=%LIB%;%OPENSSL_LIB%
開始編譯
執行build_vs140.cmd即可開始
poco目錄下bin、lib目錄就是生成文件
若64位應該是bin64、lib64
bin目錄:
lib目錄:
最后整理頭文件
如json的頭文件路徑:poco\JSON\include\Poco\JSON
整理完成:
從lib及bin目錄中取出要使用的庫,bin-all及lib-all是全部的備份
libcrypto.lib及libssl.lib要用的openssl靜態庫
重點:確保openssl的頭文件項目可以找到,建議一起放在附加包含目錄中,頭文件在openssl安裝目錄中
最終整理成include、lib兩個文件夾,分別指定為附加包含目錄及附加庫目錄既可以使用
文件放在評論區中
例:使用poco發送https請求:
#include "Poco\File.h"
#include "Poco\FileStream.h"
#include "Poco\Process.h"
#include "Poco\RegularExpression.h"
#include "Poco\DateTime.h"
#include "Poco\DateTimeFormatter.h"
#include "Poco\DateTimeParser.h"
#include "Poco\Net\HTTPSClientSession.h"
#include "Poco\Net\HTTPRequest.h"
#include "Poco\Net\HTTPResponse.h"
#include "Poco\Net\InvalidCertificateHandler.h"
#include "Poco\Net\AcceptCertificateHandler.h"
#include "Poco\URI.h"
#include "Poco\Net\SSLManager.h"
#include "Poco\JSON\Object.h"
#include "Poco\JSON\Parser.h"
#include "Poco\String.h"
#include "Poco\Net\KeyConsoleHandler.h"
#include "Poco\Net\ConsoleCertificateHandler.h"
#include "Poco\SharedPtr.h"
using namespace std;
using namespace Poco;
using namespace Poco::Net;
int main(){
? ? string postString = "your json data";
?? ?SharedPtr<InvalidCertificateHandler> ptrCert = new ConsoleCertificateHandler(true);
?? ?Context::Ptr ptrContext = new Context(Context::CLIENT_USE, "", "", "", Context::VERIFY_NONE, 9, true, "ALL:!ADH:!LOW:!EXP:!MD5:@STRENGTH");
?? ?SSLManager::instance().initializeClient(0, ptrCert, ptrContext);
?? ?string url = "your url";
?? ?URI uri(url);
?? ?HTTPSClientSession client(uri.getHost(), uri.getPort());
?? ?HTTPRequest request(HTTPRequest::HTTP_POST, uri.getPath());
?? ?request.setContentType("application/json");
?? ?std::string reqBody(postString);
?? ?request.setContentLength(reqBody.length());
?? ?client.sendRequest(request) << reqBody;
?? ?HTTPResponse response;
?? ?istream& is = client.receiveResponse(response);
?? ?if (response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK)
?? ?{
?? ??? ?char* pData = new char[response.getContentLength()];
?? ??? ?is.read(pData, response.getContentLength());
?? ??? ?std::string srcString(pData, response.getContentLength());
?? ??? ?cout << srcString << endl;
?? ?}
}
實測ConsoleCertificateHandler()如果傳參為false可能會導致證書驗證錯誤時不可用
————————————————
版權聲明:本文為CSDN博主「m0_37582045」的原創文章,遵循CC 4.0 BY-SA版權協議,轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://blog.csdn.net/m0_37582045/article/details/108833122
總結
以上是生活随笔為你收集整理的VS2015编译Poco+openssl,使用Poco发送HTTPS请求的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么用u盘老毛桃破解电脑密码 u盘老毛桃
- 下一篇: 视场角计算原理