Search code examples
c#.netwebrequest

How to correctly add WebRequest header?


var webRequest = WebRequest.Create("https://vtopbeta.vit.ac.in/vtop/");
webRequest.Method = "GET";
webRequest.Timeout = 12000;
webRequest.Headers.Add("User-Agent",
    "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36");
string jsonResponse;
using (var s = webRequest.GetResponse().GetResponseStream())
{
    using (var sr = new StreamReader(s ?? throw new InvalidOperationException()))
    {
        jsonResponse = sr.ReadToEnd();
    }
}

I wrote this yesterday and it worked just fine. Today suddenly this is not working anymore. There is an almost exact question Cannot set some HTTP headers when using System.Net.WebRequest but the answer https://stackoverflow.com/a/4752359/4427870 says I cant use User-Agent header but I literally used it a day before.

The error I get: System.ArgumentException: 'The 'User-Agent' header must be modified using the appropriate property or method. Parameter name: name'

I'd like to use WebRequest and not WebClient or HttpWebRequest.


Solution

  • You need to set UserAgent directly, use CreateHttp instead which returns HttpWebRequest, then you can do:

    webRequest.UserAgent = "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36";
    

    UserAgent is a restricted header and must be set via a property, there is a list of all restricted headers here.