VS2015编译Poco+openssl,使用Poco发送HTTPS请求
Posted 浩瀚之水_csdn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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请求的主要内容,如果未能解决你的问题,请参考以下文章