Hi to all I'm trying to change the headers of a request, I'm trying to simulate a request from different browsers, languages...
The problem is when I try to change the headers I receive an exception(see below), this is the code I'm using:

HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Headers.Set(HttpRequestHeader.UserAgent, httpRequest.ServerVariables["HTTP_USER_AGENT"]);
request.Headers.Set(HttpRequestHeader.Accept, httpRequest.ServerVariables["HTTP_ACCEPT"]);
request.Headers.Set(HttpRequestHeader.Referer, httpRequest.ServerVariables["HTTP_REFERER"]);
request.Headers.Set(HttpRequestHeader.AcceptLanguage, httpRequest.ServerVariables["HTTP_ACCEPT_LANGUAGE"]);

Is this possible to do?
Thanks in advanced :'(

Exception:
"System.ArgumentException: This header must be modified using the appropriate property.\r\nParameter name: name\r\n at System.Net.WebHeaderCollection.ThrowOnRestrictedHeader(String headerName)\r\n at System.Net.WebHeaderCollection.Set(String name, String value)\r\n at System.Net.WebHeaderCollection.Set(HttpRequestHeader header, String value)\r\n at

The error you are getting is because you are trying to modify the referrer header which is restricted if you comment out that line it should work

Hi...........,
I don't think it's possible to do it in the way you are trying to do it.

I found in the follwoing link, a way to do it using the Client object
http://www.csharpfriends.com/Articles/getArticle.aspx?articleID=115

System.Net.WebClient Client = new System.Net.WebClient();
Client.Headers.Add(HttpRequestHeader.UserAgent, "user agent to send");
Client.Headers.Add(HttpRequestHeader.Accept, "string");
Client.Headers.Add(HttpRequestHeader.Referer, "string");
Client.Headers.Add(HttpRequestHeader.AcceptLanguage, "string");
Stream strm = Client.OpenRead("http://www.google.com");
StreamReader sr = new StreamReader(strm);
string line;
do
{
line = sr.ReadLine();
somestring += line;
}
while (line != null);
strm.Close();

If I do like in the example looks like is working OK, any suggestions?

This article has been dead for over six months. Start a new discussion instead.