简体   繁体   中英

How do I set the HttpRequestHeader for a HttpWebRequest?

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?

UserAgent is a property. So set it like this:

HttpWebRequest request = new HttpWebRequest();
request.UserAgent = "Mozilla/4.0";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM