日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

VS2015编译Poco+openssl,使用Poco发送HTTPS请求

發布時間:2024/9/27 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 VS2015编译Poco+openssl,使用Poco发送HTTPS请求 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

下載源碼、安裝Openssl
下載Poco源碼

git clone https://github.com/pocoproject/poco.git

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请求的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。