Modify request headers per request C# HttpClient PCL

前端 未结 2 550
不思量自难忘°
不思量自难忘° 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 12:47

    Use HttpContent.Headers. Simply create HttpContent instance with required headers and pass it to PostAsync method.

    0 讨论(0)
  • 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);
    
    0 讨论(0)
提交回复
热议问题