0

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

3
Contributors
3
Replies
6
Views
8 Years
Discussion Span
Last Post by Potato.Head
0

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

0

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

0

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 topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.