c++ 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;

 

try
     {

         transform(mac.begin(), mac.end(), mac.begin(), ::toupper);
        
         string postString = "p1=" + p1 + "&p2=" + p2 ;//这里即为传递的参数,可以用工具抓包分析,也可以自己分析,主要是form里面每一个name都要加进来  
         SSLInitializer sslInitializer;

         SharedPtr<InvalidCertificateHandler> ptrCert = new ConsoleCertificateHandler(false);
         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 = "https://www.xxx.com/";
         URI uri(url);
         HTTPSClientSession client(uri.getHost(), uri.getPort());
         HTTPRequest request(HTTPRequest::HTTP_POST, uri.getPath());
         request.setContentType("application/x-www-form-urlencoded;charset=UTF-8");
         std::string reqBody(postString);
         request.setContentLength(reqBody.length());

         request.set("headerkey",postString);// 
          request.set("USER-AGENT", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:35.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"); 
          client.sendRequest(request) << reqBody;//发送HTTP请求到指定的服务器 
          HTTPResponse response; 
          istream& is = client.receiveResponse(response);//声明一个引用,接收HTTP请求响应 // 
          if (response.getStatus() == Poco::Net::HTTPResponse::HTTP_OK) 
          { 
            log("response HTTP_OK contentLength" + std::to_string(response.getContentLength())); 
            char* pData = new char[response.getContentLength()]; 
            is.read(pData, response.getContentLength()); 
            std::string srcString(pData, response.getContentLength()); 
            log("httppost-result2=" + srcString); 
            return DeserializeResponse(srcString); 
          } 
          catch (Poco::Exception ex) 
          { 
            log( " Message=" + ex.message()); /* 
          }

 

from:http://stackoverflow.com/questions/10875938/how-to-use-openssl-in-poco-c-library-correctly

posted on 2016-11-11 11:34  神奇的旋风  阅读(6359)  评论(0编辑  收藏  举报

导航