我正在尝试连接到端口443(SSL)上的 api.poloniex.com 。他们在该端口上使用WebSocket / Wamp服务器来推送数据。我已经尝试连接 Autobahn | JS 并且它有效。
在数据传输开始之前,会向服务器发送 Http-Upgrade-Request ,表示客户端想要从 http切换到websocket 。然后,服务器通常使用 101 - 切换协议进行响应。
使用 Autobahn | JS 库,请求如下所示:
请求:
GET / HTTP/1.1
Host: api.poloniex.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Sec-WebSocket-Version: 13
Origin: ...
Sec-WebSocket-Protocol: wamp.2.json
Sec-WebSocket-Extensions: permessage-deflate
Sec-WebSocket-Key: 0n4ibEwIyTMDMhKoxXt+Cg==
Cookie: ......
Connection: keep-alive, Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
响应:
HTTP/1.1 101 Switching Protocols
Server: Crossbar/0.10.4
X-Powered-By: AutobahnPython/0.10.4
Upgrade: WebSocket
Connection: Upgrade
Sec-WebSocket-Protocol: wamp.2.json
Sec-WebSocket-Accept: dbaqDVdVW+xtbCa2V0yjtb7yXqI=
我尝试手动发送请求标头。我使用openssl s_client -connect api.poloniex.com:443
连接SSL加密。然后我发送了一个稍微修改过的标题(我删除了Origin和Cookie):
GET / HTTP/1.1
Host: api.poloniex.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: de,en-US;q=0.7,en;q=0.3
Accept-Encoding: gzip, deflate
Sec-WebSocket-Version: 13
Sec-WebSocket-Protocol: wamp.2.json
Sec-WebSocket-Extensions: permessage-deflate
Sec-WebSocket-Key: czW+w3Z+3yZCKVGx6DQ1Rg==
Connection: keep-alive, Upgrade
Pragma: no-cache
Cache-Control: no-cache
Upgrade: websocket
然而,服务器没有回答。它甚至没有关闭连接,只是没有发生。我也尝试将Cookie和Origin字段保留在其中,但这也不起作用。
编辑:以这种方式发送邮件也不起作用:
GET / HTTP/1.1\r\nHost: api.poloniex.com\r\nUser-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0\r\nAccept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\nAccept-Language: de,en-US;q=0.7,en;q=0.3\r\nAccept-Encoding: gzip, deflate\r\nSec-WebSocket-Version: 13\r\nSec-WebSocket-Protocol: wamp.2.json\r\nSec-WebSocket-Extensions: permessage-deflate\r\nSec-WebSocket-Key: czW+w3Z+3yZCKVGx6DQ1Rg==\r\nConnection: keep-alive, Upgrade\r\nPragma: no-cache\r\nCache-Control: no-cache\r\nUpgrade: websocket\r\n\r\n
答案 0 :(得分:1)
HTTP请求的行结尾为\r\n
,但通常会接受单\n
行。但你可能错过的是最后一个空行,它是标题标记的结尾。因此,服务器仍然等待标题的结束。