ClientWebSocket with parameters in url in C#

rahul saxena 1 Reputation point
2021-02-18T07:12:30.583+00:00

0

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://abc.com/api/st/co/? 
                               token=c2c49d92asf9b44925ec5f43b0ff5d9fdaeahdfjsbf2";
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://abc.com/api/st/co/?token=" + 
                 System.Net.WebUtility.UrlEncode("c2c49d92asf9b44925ec5f43b0ff5d9fdaeahdfjsbf2");

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

Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Alberto Poblacion 1,571 Reputation points
    2021-02-18T15:54:55.907+00:00

    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.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.