harun jasna
harun jasna

Reputation: 3

With C# Websocket4Net, is there a way to add request headers when connecting?

I tried looking everywhere for the solution, but I wasn't able to find out how to do it.

WebSocket websocket = new WebSocket("wss://url.com");

//How would I add custom headers around here, like User-Agent etc?

websocket.Opened += (sender1, e2) =>
{
    MessageBox.Show("Connected to WebSocket server");
};

websocket.Open();

I tried looking around and hopped around 2-3 different websocket packages but it seems like they all either couldn't handle my websocket connection properly or they didn't support custom headers. Any help would be appreciated.

Upvotes: 0

Views: 309

Answers (1)

Dekryptid
Dekryptid

Reputation: 1110

To add custom headers with WebSocket4Net, you can pass them in the constructor of the WebSocket instance:

WebSocket websocket = new WebSocket("wss://url.com", customHeaderItems: new List<KeyValuePair<string, string>> {
    new KeyValuePair<string, string>("User-Agent", "YourCustomUserAgent"),
    new KeyValuePair<string, string>("Another-Header", "HeaderValue")
});

If Websocket4Net does not support this directly in the version you're using, or if the constructor does not allow custom headers, you may need to check the documentation or repo for the version you're using.

Upvotes: 0

Related Questions