How to install default user agent on HttpClient?

It's easy to install the user agent in HttpRequest, but often I want to use the same HttpClient and use the same user agent each time, instead of setting it for each request.

+7
source share
2 answers

You can easily solve this problem using:

HttpClient _client = new HttpClient(); _client.DefaultRequestHeaders.Add("User-Agent", "C# App"); 
+26
source

The following is the code to change the User Agent in a controller action.

 public class HomeController : Controller { public IActionResult Index() { HttpContext.Request.Headers["User-Agent"] = "MyAgent/1.0"; return View(); } } 
-nine
source

All Articles