How to set User Agent with System.Net.WebRequest in c#

前端 未结 3 1015
遇见更好的自我
遇见更好的自我 2020-12-31 11:15

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

3条回答
  •  旧巷少年郎
    2020-12-31 11:41

    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.

提交回复
热议问题