2016-02-22 99 views
1

我使用cpprestsdk「卡薩布蘭卡」主分支HTTPS URL,它的工作在Windows和OSX但是當我在linux上運行它,我收到「錯誤是SSL握手」如何解決cpprestsdk中的「ssl握手錯誤」?

C++ exception with description "Error in SSL handshake" thrown in the test body. 

我試圖打開這個它使用Firefox的網址。

當我與HTTP URL中使用它一切正常 我檢查在客戶端,我發現代碼此消息在一個文件名爲「http_client_asio.cpp」

void write_request() 
    { 
     // Only perform handshake if a TLS connection and not being reused. 
     if (m_connection->is_ssl() && !m_connection->is_reused()) 
     { 
      const auto weakCtx = std::weak_ptr<asio_context>(shared_from_this()); 
      m_connection->async_handshake(boost::asio::ssl::stream_base::client, 
              m_http_client->client_config(), 
              m_http_client->base_uri().host(), 
              boost::bind(&asio_context::handle_handshake, shared_from_this(), boost::asio::placeholders::error), 

              // Use a weak_ptr since the verify_callback is stored until the connection is destroyed. 
              // This avoids creating a circular reference since we pool connection objects. 
              [weakCtx](bool preverified, boost::asio::ssl::verify_context &verify_context) 
              { 
               auto this_request = weakCtx.lock(); 
               if(this_request) 
               { 
                return this_request->handle_cert_verification(preverified, verify_context); 
               } 
               return false; 
              }); 
     } 
     else 
     { 
      m_connection->async_write(m_body_buf, boost::bind(&asio_context::handle_write_headers, shared_from_this(), boost::asio::placeholders::error)); 
     } 
    } 

    void handle_handshake(const boost::system::error_code& ec) 
    { 
     if (!ec) 
     { 
      m_connection->async_write(m_body_buf, boost::bind(&asio_context::handle_write_headers, shared_from_this(), boost::asio::placeholders::error)); 
     } 
     else 
     { 
      report_error("Error in SSL handshake", ec, httpclient_errorcode_context::handshake); 
     } 
    } 

我craeted HTTP客戶端這樣的

http_client client(U("https://www.bing.com/")); 

我該如何解決這個錯誤?

+0

[相關](http://stackoverflow.com/questions/28264313/ssl-certificates-and-b奧斯特-ASIO)。 –

+0

你可以添加一個斷點並添加更多關於導致消息的'ec'對象的信息嗎?另外,你有沒有嘗試其他網站?如果您可以通過http站點重現問題,則可以嘗試使用wireshark檢查網絡通信。 – roschuma

回答

1

我試圖連接到https URL時遇到了同樣的問題。

首先,如果你不需要連接到https,你可以將你的url替換爲http。

另一種解決方案是將http_client_config憑據驗證設置爲false。事情是這樣的:

http_client_config config; 
    config.set_validate_certificates(false); 

但是,如果你需要做一個HTTPS連接與包括安全性,您必須包括當地的證書文件(通常是CRT,或DEM文件),把它作爲你的http_client配置回調:

http_client_config config; 
    config.set_ssl_context_callback([]((boost::asio::ssl::context &ctx) { 
     ctx.load_verify_file("PATH_TO_CERTIFICATE_FILE"); 
    }); 

你可以看到此處詳細瞭解這些功能:

https://microsoft.github.io/cpprestsdk/classweb_1_1http_1_1client_1_1http__client__config.html