views:

39

answers:

1

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?

+2  A: 

UserAgentis a property. So set it like this:

HttpWebRequest request = new HttpWebRequest();
request.UserAgent = "Mozilla/4.0";
Simon Linder
As the standard constructor is obsolet, try HttpWebRequest myReq = (HttpWebRequest)WebRequest.Create(myUrl);
Simon Linder
Will do. Thank you.
Mr. Flibble