1

I need to create a websocket to a particular url(cannot reveal) in c#. Is there something similar to the WebSocket API in Java, or libwebsockets in C for C#?

I have already tried using WebSocketSharp and ChilKat by following some of the SO answers. Also tried using the Microsofts WebSocket Namespace. But was always getting the "Not Authorized" error 401. I also tried doing an Http get and tried to add headers to upgrade the socket to Websocket by following the tutorial to create WebSocket servers in the MDN docs, but was only getting a redirected webpage in return. This is the code I used for the HTTP upgrade request. I am a beginner to c#.

ServicePointManager.Expect100Continue = true;
                ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls
                                                    | SecurityProtocolType.Tls11
                                                    | SecurityProtocolType.Tls12
                                                    | SecurityProtocolType.Ssl3;
            ServicePointManager.ServerCertificateValidationCallback += (se, cert, chain, sslerror) =>
            {
                return true;
            };
            var http = (HttpWebRequest)WebRequest.Create(new Uri(baseAddress));
            http.AllowAutoRedirect = false;
            http.UseDefaultCredentials = true;
            http.PreAuthenticate = true;
            http.Credentials = CredentialCache.DefaultCredentials;
            http.Proxy.Credentials = CredentialCache.DefaultCredentials;
            http.Accept = "application/json";
            http.ContentType = "application/json";
            http.Method = "GET";
            http.Connection = "Open";
            http.Headers.Add("Authorization", "Bearer " + Token);
            //http.Headers.Add("Connection", "Upgrade");
            http.Headers.Add("Upgrade", "websocket");
            http.KeepAlive = true;

What I want to achieve is something like,

Websocket socket = new Websocket();
socket.addHeader(Authorization, Bearer, Token);
socket.connect(); 

Thanks in Advance for any inputs.

  • have you looked at httpclient? – BugFinder Jun 13 '19 at 11:59
  • websockets don't have http headers. depending on your server implementation you may pass a token in the query string. – Markus Dresch Jun 13 '19 at 12:04
  • @MarkusDresch I assumed I could maybe start with http header and then upgrade it to a websocket, I got this idea after reading this [link](https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_server) – Apoorva Eliza John Jun 13 '19 at 12:47
  • @BugFinder I did not find any examples with httpclient, so No. – Apoorva Eliza John Jun 13 '19 at 12:49
  • There are numerous examples of httpclient sending headers. have a look :) its where I found them when i was wondering how to do the same thing – BugFinder Jun 13 '19 at 13:37
  • if you are using dotnet core, you may want to check out ClientWebSocket in System.Net.WebSockets, especially the Options.SetRequestHeader method. – Markus Dresch Jun 13 '19 at 13:58
  • Thankyou for your suggestions. I am using .Net framework 4.5 in windows 7. From my searching around seems like I will not be able to use Microsofts Websockets.ClientWebsocket .. I have decided to go with Websocket4Net. – Apoorva Eliza John Jun 19 '19 at 09:27

1 Answers1

4

I am using ClientWebSocket to connect to the Zendesk chat streaming API using an OAuth token like this:

var webSocket = new ClientWebSocket();
webSocket.Options.SetRequestHeader("Authorization", $"Bearer {_oauthToken}");
var cts = new CancellationTokenSource();
await webSocket.ConnectAsync(new Uri(url), cts.Token);

It works for me!

lukiller
  • 1,107
  • 9
  • 12