【发布时间】:2011-10-26 03:48:15
【问题描述】:
我正在尝试从我的 C++ 应用程序中读取 https://mtgox.com/api/0/data/ticker.php 的股票代码。 我使用 Boost.Asio 和 OpenSSL,因为该服务需要 HTTPS。
加速版本:1.47.0
OpenSSL:1.0.0d [2011 年 2 月 8 日] Win32
对于应用程序;我以http://www.boost.org/doc/libs/1_47_0/doc/html/boost_asio/example/ssl/client.cpp 为例 开始并修改如下:
这是我要连接的地方:
boost::asio::ip::tcp::resolver::query query("mtgox.com", "443");
我将验证设置为无,否则握手会失败。我不确定这是否是 mtgox 的问题,或者这个实现是否真的很严格,因为当我将证书打印到屏幕上时,它看起来是合法的(访问代码页面时 chrome 没有问题)。
socket_.set_verify_mode(boost::asio::ssl::context::verify_none);
这是我发送的请求:
std::stringstream request_;
request_ << "GET /api/0/data/ticker.php HTTP/1.1\r\n";
request_ << "Host: mtgox.com\r\n";
request_ << "Accept-Encoding: *\r\n";
request_ << "\r\n";
boost::asio::async_write(socket_, boost::asio::buffer(request_.str()), boost::bind(&client::handle_write, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
(完整代码:http://pastebin.com/zRTTqZVe)
我遇到以下错误:
Connection OK!
Verifying:
/C=IL/O=StartCom Ltd./OU=Secure Digital Certificate Signing/CN=StartCom Certification Authority
Sending request:
GET /api/0/data/ticker.php HTTP 1.1
Host: mtgox.com
Accept-Encoding: *
Sending request OK!
Read failed: An existing connection was forcibly closed by the remote host
我是否朝着正确的方向前进?错误消息并没有真正描述问题,我不知道我做错了哪一步。
更新: 我用 cURL 看看出了什么问题:
curl --trace-ascii out.txt https://mtgox.com/api/0/data/ticker.php
(完整输出:http://pastebin.com/Rzp0RnAK) 在验证过程中失败。 当我连接“不安全”参数时
curl --trace-ascii out.txt -k https://mtgox.com/api/0/data/ticker.php
(完整输出:http://pastebin.com/JR43A7ux)
一切正常。
修复:
- 我修正了 HTTP 标头中的错字
- 我添加了根证书并 重新开启 SSL 验证。
【问题讨论】:
-
为什么要生成密钥?你没有签署任何东西,服务器是。不过,您需要信任服务器的根证书。
-
您是否尝试过发送该请求,例如通过 curl 来查看服务器是否接受它?
-
好主意!我已经更新了问题。
标签: c++ boost https openssl boost-asio