Fro*_*art 1 c++ websocket websocket++
我无法通过名为websocketpp的 WebSockets C++库从MtGox API获取信息:
#include <websocketpp/config/asio_no_tls_client.hpp>
#include <websocketpp/client.hpp>
#include <iostream>
typedef websocketpp::client<websocketpp::config::asio_client> client;
using websocketpp::lib::placeholders::_1;
using websocketpp::lib::placeholders::_2;
using websocketpp::lib::bind;
typedef websocketpp::config::asio_client::message_type::ptr message_ptr;
void on_open(websocketpp::connection_hdl hdl)
{
std::cout << "on_open \n";
}
void on_close(websocketpp::connection_hdl hdl)
{
std::cout << "on_close \n";
}
void on_message(client* c, websocketpp::connection_hdl hdl, message_ptr msg)
{
std::cout << msg->get_payload() << '\n';
}
int main()
{
client c;
try
{
c.init_asio();
c.set_open_handler(on_open);
c.set_close_handler(on_close);
c.set_message_handler(bind(&on_message, &c, ::_1, ::_2));
websocketpp::lib::error_code ec;
client::connection_ptr con = c.get_connection("ws://websocket.mtgox.com:80/mtgox?Currency=EUR", ec);
c.connect(con);
c.run();
}
catch (const std::exception& e)
{
std::cout << e.what() << std::endl;
}
catch (websocketpp::lib::error_code e)
{
std::cout << e.message() << std::endl;
}
catch (...)
{
std::cout << "other exception" << std::endl;
}
}
Run Code Online (Sandbox Code Playgroud)
产量
[2013-11-18 23:10:10] [connect]连接成功
[2013-11-18 23:10:14] [错误]服务器握手响应无效:HTTP状态无效.
[2013-11-18 23:10:14] [disconnect]失败:HTTP状态无效.
在调试器中,我看到"403 forbidden"错误,但我可以通过http://www.websocket.org/echo.html等服务使用它.
我已经尝试使用"ws://socketio.mtgox.com:80/mtgox?Currency = EUR",但收到以下错误:
[2013-11-18 23:18:07] [connect]成功连接
[2013-11-18 23:18:08] handle_read_http_response中的[error]错误:文件结束
[2013-11-18 23:18:08] [disconnect]失败:文件结束
这段代码出了什么问题?
MtGox似乎正在进行原点过滤.基于浏览器的WebSocket连接将具有自动发送的源头,其中包含运行脚本的域的值.由于这主要是针对运行可能未知的Javascript代码的浏览器的安全措施,因此默认情况下WebSocket ++不会发送原始标头.
只要有一个设置,MtGox似乎可以正常使用我尝试过的任何来源.据推测,他们使用这个来列出他们认为是恶意的起源.您可以使用WebSocket ++使用以下代码发送原始标头(填写适合您的应用程序的任何原点):
con->replace_header("Origin","http://www.example.com");
Run Code Online (Sandbox Code Playgroud)
请求新连接后endpoint::get_connection,但在呼叫之前运行此命令endpoint::connect.
有关此处使用的"同源策略"安全方法的更多详细信息,请参见http://en.wikipedia.org/wiki/Same-origin_policy.
| 归档时间: |
|
| 查看次数: |
786 次 |
| 最近记录: |