ClientWebSocket with parameters in url in C#

rahul saxena 1 Reputation point


I am trying to connect a websocket with parameters in URL. Same url I can connect from python without any issues. But from c# I'm getting exception when I do the ConnectAsync Unable to connect to the remote server

Code Snippet:

System.Net.WebSockets.Managed.ClientWebSocket _clientWebSocket;
_clientWebSocket = new System.Net.WebSockets.Managed.ClientWebSocket();
var marketdatawithT = "wss:// 
await _clientWebSocket.ConnectAsync(new Uri(marketdatawithT ), CancellationToken.None);

Any help why I'm not able to connect a WebSocketwith url as a parameter from c#? I can connect to endpoint which does not have URL without any issues. I have also tried to encode the url in token like below:

var marketdatawithT = "wss://" + 

But still see the same issue. Any help will be highly appreciated.

An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,638 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Alberto Poblacion 1,556 Reputation points

    Maybe this is just a problem from copying and pasting your code into the forum. But you are doing

    var marketdatawithT = "..."; NOTE the "withT" at the end

    and then

    await _clientWebSocket.ConnectAsync(new Uri(marketdata), ...); NOTE no "withT"

    I.e., you are making the call with a different variable, which may not contain the expected token.

    No need for UrlEncode; your token only contains letters and numbers, not anything that has to be encoded.