强制HttpClient的使用内容类型:文本/ XML [英] Forcing HttpClient to use Content-Type: text/xml

查看:812
本文介绍了强制HttpClient的使用内容类型:文本/ XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我发疯,我设置ContentType标头到处都是我,似乎能不能让它停止发送文字/平原。



看着提琴手数据,请求总是要求:



POST HTTP:/domain.com HTTP /1.1



的Content-Type:text / plain的;字符集= UTF-8

 使用(VAR的HttpClient =新的HttpClient())
{
变种要求=新HttpRequestMessage(HttpMethod.Posthttp://domain.com);
request.Content =新的StringContent(序列化(OBJ),Encoding.UTF8,文/ XML);
request.Content.Headers.Clear();
request.Content.Headers.ContentType =新MediaTypeHeaderValue(文/ XML);
request.Headers.Clear();
request.Headers.Add(内容类型,文本/ XML);
VAR响应=等待httpClient.SendAsync(请求);
返回等待response.Content.ReadAsStringAsync();
}


解决方案

它看起来像你试图硬:)这应该只是工作。

 使用(VAR的HttpClient =新的HttpClient())
{
VAR要求=新HttpRequestMessage(HttpMethod.Posthttp://domain.com);
request.Content =新的StringContent(序列化(OBJ),Encoding.UTF8,文/ XML);
VAR响应=等待httpClient.SendAsync(请求);
返回等待response.Content.ReadAsStringAsync();
}


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 request is always requesting:

POST http:/domain.com HTTP/1.1

Content-Type: text/plain; charset=utf-8

using (var httpClient = new HttpClient())
{
    var request = new HttpRequestMessage(HttpMethod.Post, "http://domain.com");
        request.Content = new StringContent(Serialize(obj), Encoding.UTF8, "text/xml");
        request.Content.Headers.Clear();
        request.Content.Headers.ContentType = new MediaTypeHeaderValue("text/xml");
        request.Headers.Clear();
        request.Headers.Add("Content-Type","text/xml");
        var response = await httpClient.SendAsync(request);
        return  await response.Content.ReadAsStringAsync();
}

解决方案

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();
}

这篇关于强制HttpClient的使用内容类型:文本/ XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆