views:

69

answers:

5

Hi,

How can I change the HttpContext.Current.Request.UserAgent property using reflection?

Thanks

A: 
Femaref
-1 it's a get only property
Tim Mahy
fixed it, looked up the wrong class
Femaref
there even is no private field behind it, you can use a tool like Reflector to get some insights in the code behind this property...
Tim Mahy
Maybe change the header before Request.UserAgent (and internal logic) uses it?
Andreas
A: 

You have to use HttpWebRequest

HttpWebRequest.UserAgent Property

Keivan
A: 

You can't modify the incoming request's user agent. It is the value that is sent by the users browser so I'm not sure why you would want to change it anyways. If you were creating an outbound request yourself using the HttpWebRequest class you could set its .UserAgent property, or you could add to the .Headers collection if you were using a WebClient class.

thekaido
A: 

HttpContext.Current.Request is read only and so does HttpContext.Current.Request.UserAgent. This is so by nature of web, there's no reason why you may want to change anything within HttpRequest object since this has sent by some HttpClient on which you may never have control. Even in a case when you could change it, you're losing the integrity of the original Request which is obviously not recommended. I don't know what you're trying to achieve by changing the Request object or its properties, but I know for sure that's not the way to go. Try to work out some other way instead of changing the Request object, or write your actual problem statement here and we'll try to help you out.

this. __curious_geek
A: 

I solved it by using a ashx (as a proxy) and making a request from that service to the page with a custom useragent header and then printed the answer I got from the other page.

Andreas