6

I'm trying to set the HttpRequestHeader for a HttpWebRequest like so:

new HttpWebRequest().Headers.Add(HttpRequestHeader.UserAgent, "Mozilla/4.0");

But I get an exception: System.ArgumentException: This header must be modified using the appropriate property.

How should I be setting the header?

Mr. Flibble
  • 26,564
  • 23
  • 69
  • 100

1 Answers1

9

UserAgentis a property. So set it like this:

HttpWebRequest request = new HttpWebRequest();
request.UserAgent = "Mozilla/4.0";
Simon Linder
  • 3,378
  • 4
  • 32
  • 49
  • 1
    As the standard constructor is obsolet, try HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(myUrl); – Simon Linder Mar 18 '10 at 15:07
  • @Emixam23 have a look at https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.contentlength(v=vs.110).aspx – Simon Linder Jun 16 '16 at 17:06
  • I finaly found another solution, ContentLenght doesn't works for HttpWebReuqest Xamarin, there is the solution : http://stackoverflow.com/questions/37837204/xamarin-forms-cannot-set-the-contentlenght-of-httpwebrequest?noredirect=1#comment63172228_37837204 – Emixam23 Jun 17 '16 at 08:05
  • how can you set the -u "--user" property in the same way as the UserAgent property? – Joshua Vandenbor Aug 13 '19 at 19:20