views:

5960

answers:

5

Hi,

When I try to set the header properties on a WebRequest object, I get the following exception

This header must be modified using the appropriate property

I've tried modifying the .Headers propery and adding them that way like so

webRequest.Headers.Add(HttpRequestHeader.Referer, "http://stackoverflow.com");

But still getting exceptions. I can get around this by casting the WebRequest object to a HttpWebRequest and setting the properties such as httpWebReq.Referer ="http://stackoverflow.com"

But I'd like to know if there's a way to get a finer grained control over modifying headers with a request for a remote resource.

Thanks

+2  A: 

WebRequest being abstract (and since any inheriting class must override the Headers property).. which concrete WebRequest are you using ? In other words, how do you get that WebRequest object to beign with ?

ehr.. mnour answer made me realize that the error message you were getting is actually spot on: it's telling you that the header you are trying to add already exist and you should then modify its value using the appropriate property (the indexer, for instance), instead of trying to add it again. That's probably all you were looking for.

Other classes inheriting from WebRequest might have even better properties wrapping certain headers; See this post for instance.

FOR
Actually WebRequest.Create(url) creates an instance of a WebRequest object.
hmemcpy
A: 

Im creating a WebRequest object by WebRequest.Create()

I have already tried modifying the existing header using webReq.Headers[HttpWebRequest.Referer] = "a value"

I get a different error System.ArgumentException: This header must be modified using the appropriate property

Although its not a show stopper, more granular control over setting header properties would be nice. Anyone?

I also read that post. Sort of said what I already know.

Sir Psycho
+1  A: 

Basically, no. That is an http header, so it is reasonable to cast to HttpWebRequest and set the .Referer (as you indicate in the question):

HttpWebRequest req = ...
req.Referer = "your url";
Marc Gravell
+1  A: 

Anytime you're changing the headers of an HttpWebRequest, you need to use the appropriate properties on the object itself, if they exist. If you have a plain WebRequest, be sure to cast it to an HttpWebRequest first. Then "Referrer" in your case can be accessed via "((HttpWebRequest)request).Referrer", so you don't need to modify the header directly - just set the property to the right value. ContentLength, ContentType, UserAgent, etc, all need to be set this way.

IMHO, this is a shortcoming on MS part...setting the headers via Headers.Add() should automatically call the appropriate property behind the scenes, if that's what they want to do.

jvenema
+1  A: 

I ran into this problem with a custom web client. I think people may be getting confused because of multiple ways to do this. When using WebRequest.Create() you can cast to an HttpWebRequest and use the property to add or modify a header. When using a WebHeaderCollection you may use the .Add("referer","my_url")

Ex 1

            WebClient client = new WebClient();
            client.Headers.Add("referer", "http://stackoverflow.com");
            client.Headers.Add("user-agent", "Mozilla/5.0");

Ex 2

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            request.Referer = "http://stackoverflow.com";
            request.UserAgent = "Mozilla/5.0";
            response = (HttpWebResponse)request.GetResponse();
Chmod