dimanche 25 octobre 2015

Why is a different URL trying to be visited when connecting to this web socket in Java?

I'm trying to connect to a web socket in java using the Kaazing WebSocket Gateway. I'm using the following code to connect to a web socket:

WebSocketFactory wsFactory = WebSocketFactory.createWebSocketFactory();
WebSocket ws = wsFactory.createWebSocket(URI.create("ws://example.com:8080"));
ws.connect();

The error I'm getting is that the response from the server is 400 even though I know the server accepts web sockets as I have a site that uses it.

So I opened up wireshark and saw that the request sent to the server was a bit weird, it looked like this:

Hypertext Transfer Protocol
    GET /;e/cookies?.krn=5044049923726918&.kl=Y HTTP/1.1\r\n
    User-Agent: Java/1.8.0_45\r\n
    Host: example.com:8080\r\n
    Accept: text/html, image/gif, image/jpeg, *; q=.2, */*; q=.2\r\n
    Connection: keep-alive\r\n
    \r\n
    [Full request URI: http://ift.tt/1i5IIye]
    [HTTP request 1/1]
    [Response in frame: 962]

If I compare this to one that is made by my site to the server you can see there is a difference:

Hypertext Transfer Protocol
    GET / HTTP/1.1\r\n
    Upgrade: websocket\r\n
    Connection: Upgrade\r\n
    Host: example.com:8080\r\n
    Origin: http://example.com:8080\r\n
    Sec-WebSocket-Protocol: login\r\n
    Cookie: rememberme=true\r\n
    Pragma: no-cache\r\n
    Cache-Control: no-cache\r\n
    Sec-WebSocket-Key: RABFhQOLbxz2itPZi9HYXA==\r\n
    Sec-WebSocket-Version: 13\r\n
    Sec-WebSocket-Extensions: x-webkit-deflate-frame\r\n
    User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11) AppleWebKit/601.1.56 (KHTML, like Gecko) Version/9.0 Safari/601.1.56\r\n
    \r\n
    [Full request URI: http://ift.tt/1Fus9Ux]
    [HTTP request 1/1]
    [Response in frame: 3506]

So my question is how can I replicate this in Java. The Java code will be a desktop application which will have dome of the features of the site.

Would it be better if I used a different Java web socket library or is it possible to do it using this one?




Aucun commentaire:

Enregistrer un commentaire