Modify request headers per request C# HttpClient PCL

前端 未结 2 551
不思量自难忘°
不思量自难忘° 2020-12-28 12:25

I\'m currently using the System.Net.Http.HttpClient for cross platform support.

I read that it is not a good practice to instantiate a HttpClient object for each req

2条回答
  •  旧巷少年郎
    2020-12-28 13:09

    Yes, you can create a new HttpRequestMessage, set all the properties you need to, and then pass it to SendAsync.

    var request = new HttpRequestMessage() {
       RequestUri = new Uri("http://example.org"),
       Method = HttpMethod.Post,
       Content = new StringContent("Here is my content")
    }
    request.Headers.Accept.Add(...);  // Set whatever headers you need to
    
    var response = await client.SendAsync(request);
    

提交回复
热议问题