1

I need to collect data from a WebSocket.

If I try to test the websocket with this site: http://www.websocket.org/echo.html I can connect and send message without problem but when I try to connect to the websocket using .Net Framework libraries I get:

System.Net.WebException: Internal Server Error (500).

The code I use to connect is this:

        string uri = "wss://wss.xxx.xxx";
        ClientWebSocket webSocket = null;
        try
        {
            webSocket = new ClientWebSocket();
            await webSocket.ConnectAsync(new Uri(uri), CancellationToken.None);
            await Task.WhenAll(Receive(webSocket), Send(webSocket));
        }
        catch (Exception ex)
        {
            Console.WriteLine("Exception: {0}", ex);
        }

If I try to connect to any other web socket, the code works well...

The difference with the other websocket is that the address starts with "wss" instead of "ws" ( think its because of secure connection).

If I test with a browser like Chrome the websocket connetion works, but not in .Net, may be I have to add some header in order to simulate a Browser Agent ?

Thanks to support

3
  • I've tried your code with ws://echo.websocket.org it works fine. Commented Dec 28, 2016 at 8:03
  • Hi Memmet, yes, it works for ws://echo.websocket.org but not for the websocket that I have to use. Commented Dec 28, 2016 at 8:59
  • Hi Memmet, yes, it works for ws://echo.websocket.org but not for the websocket that I have to use. If you check my answer, now I can connect, I just had to add some headers and the handshake works... Commented Dec 28, 2016 at 9:00

1 Answer 1

1

Finally After some search I find out the problem: I just had to add headers to the ClientWebSocket.

The problem is that ClientWebSocket doesn't provide methods to add specific headers and I had to make a force of ClientWebSocket.

This thread help me a lot to solve my problem:

Setting "User-Agent" HTTP header in ClientWebSocket

Using this new version of ClientWebSocket I was able to add Headers.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.