Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I set User-Agent and Referer headers when using ClientWebSocket in .net 4.5?

The obvious answer of using ClientWebSocket.SetHeader throws an exception because it's a protected header:

System.ArgumentException occurred
  Message=The 'User-Agent' header must be modified using the appropriate property or method.
Parameter name: name
  ParamName=name
  StackTrace:
       at System.Net.WebHeaderCollection.ThrowOnRestrictedHeader(String headerName)

The exception string suggests using a property/method on the ClientWebSocket itself but I can't find any such property/method. It seems this exception was designed for the HttpWebRequest class, which actually has such property.

The code, which doesn't work:

ClientWebSocket socket = new ClientWebSocket();
// Will throw
socket.Options.SetRequestHeader("User-Agent", "SomeUserAgentString");
// Will throw
socket.Options.SetRequestHeader("Referer", "SomeReferer"]);
like image 613
Palo Avatar asked Nov 03 '22 00:11

Palo


1 Answers

It doesn't look like you'll be able to set those properties, at least not right now. You might be able to do it via reflection.

If you look closely at your stack trace, you'll see that the throwing method is System.Net.WebHeaderCollection.ThrowOnRestrictedHeader. System.Net.WebHeaderCollection is a specialized name value collection designed to deal with HTTP headers. If you look at the remarks section, you'll see the following:

Some common headers are considered restricted and are either exposed directly by the API (such as Content-Type) or protected by the system and cannot be changed.

The list has both the User-Agent and Referer properties listed as protected headers and cannot be set since the ClientWebSocket does not expose it.

All that being said, though, if you absolutely need to set those headers, you'll need to find the private reference WebHeaderCollection of your ClientWebSocketOptions (exposed as the Options property on your ClientWebSocket) and call the protected AddWithoutValidate method to set the headers.

like image 196
Joshua Avatar answered Nov 14 '22 01:11

Joshua