Forcing HttpClient to use Content-Type: text/xml

后端 未结 3 1048
一整个雨季
一整个雨季 2021-01-04 00:25

This is driving me nuts, I am setting the ContentType header everywhere I can and can\'t seem to make it stop sending text/plain.

Watching the data in Fiddler, the

相关标签:
3条回答
  • 2021-01-04 00:36

    It looks like you tried to hard :) This should just work.

    using (var httpClient = new HttpClient())
    {
        var request = new HttpRequestMessage(HttpMethod.Post, "http://domain.com");
        request.Content = new StringContent(Serialize(obj), Encoding.UTF8, "text/xml");
        var response = await httpClient.SendAsync(request);
        return  await response.Content.ReadAsStringAsync();
    }
    
    0 讨论(0)
  • 2021-01-04 00:37

    Try settings the default request headers:

    client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("text/xml"));
    
    0 讨论(0)
  • 2021-01-04 00:49

    Use "application/xml" instead of "text/xml"

    0 讨论(0)
提交回复
热议问题