c# - How can I set User-Agent and Referer headers when using ClientWebSocket in .net 4.5? -
the obvious answer of using clientwebsocket.setheader
throws exception because it's protected header:
system.argumentexception occurred message=the 'user-agent' header must modified using appropriate property or method. parameter name: name paramname=name stacktrace: @ system.net.webheadercollection.throwonrestrictedheader(string headername)
the exception string suggests using property/method on clientwebsocket
can't find such property/method. seems exception designed httpwebrequest
class, has such property.
the code, doesn't work:
clientwebsocket socket = new clientwebsocket(); // throw socket.options.setrequestheader("user-agent", "someuseragentstring"); // throw socket.options.setrequestheader("referer", "somereferer"]);
it doesn't you'll able set properties, @ least not right now. might able via reflection.
if closely @ stack trace, you'll see throwing method system.net.webheadercollection.throwonrestrictedheader
. system.net.webheadercollection
specialized name value collection designed deal http headers. if @ remarks section, you'll see following:
some common headers considered restricted , either exposed directly api (such content-type) or protected system , cannot changed.
the list has both user-agent
, referer
properties listed protected headers , cannot set since clientwebsocket
not expose it.
all being said, though, if absolutely need set headers, you'll need find private reference webheadercollection
of clientwebsocketoptions
(exposed options
property on clientwebsocket
) , call protected addwithoutvalidate
method set headers.
Comments
Post a Comment