Hi I\'m trying to set User Agent with WebRequest, but unfortunately I\'ve only found how to do it using HttpWebRequest, so here is my code and I hope you can help me to set
Use the UserAgent property on HttpWebRequest
by casting it to a HttpWebRequest
.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = "my user agent";
Alternatively, instead of casting you can use WebRequest.CreateHttp instead.
If you try using a HttpWebRequest
instead of a basic WebRequest, then there is a specific property exposed for UserAgent.
// Create a new 'HttpWebRequest' object to the mentioned URL.
var myHttpWebRequest=(HttpWebRequest)WebRequest.Create("http://www.contoso.com");
myHttpWebRequest.UserAgent=".NET Framework Test Client";
// Assign the response object of 'HttpWebRequest' to a 'HttpWebResponse' variable.
var myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse();
WebRequest postrequest = WebRequest.Create("protocol://endpointurl.ext");
((System.Net.HttpWebRequest)postrequest).UserAgent = ".NET Framework"