简体   繁体   English

HttpClient在POST期间未传递内容

[英]HttpClient not passing content during POST

I'm doing a simple call using HttpClient . 我正在使用HttpClient进行简单的调用。 The problem is that it doesn't seem to send the content I provide. 问题在于它似乎没有发送我提供的内容。 The server gives me an error for this and when I look at the request with Fiddler it doesn't show the content as it should: 服务器为此给我一个错误,当我用Fiddler查看请求时,它没有按原样显示内容:

HttpContent content = new FormUrlEncodedContent(
                new List<KeyValuePair<string, string>> { 
                    new KeyValuePair<string, string>("key1", "value1"),
                    new KeyValuePair<string,string>("key2","value2"),
                    new KeyValuePair<string,string>("key3","value3"),
                    new KeyValuePair<string,string>("key4", "value4"),
                    new KeyValuePair<string, string>("key5", "value5"),
                    new KeyValuePair<string, string>("key6", "value6")
                });
content.Headers.ContentType = new MediaTypeHeaderValue("application/x-www-form-urlencoded");
content.Headers.ContentType.CharSet = "UTF-8";
HttpResponseMessage resposne = await client.PostAsync(new Uri("my_url"), content);

Any suggestions on what can be wrong? 关于什么可能是错误的任何建议?

It's possible that UserAgent is the issue. UserAgent可能是问题所在。 HttpClient on MSDN MSDN上的HttpClient

By default, no user-agent header is sent with the HTTP request to the web service by the HttpClient object. 默认情况下,HttpClient对象不会将任何用户代理标头与HTTP请求一起发送到Web服务。 Some HTTP servers, including some Microsoft web servers, require that a user-agent header be included with the HTTP request sent from the client. 某些HTTP服务器(包括某些Microsoft Web服务器)要求从客户端发送的HTTP请求中包含用户代理标头。 The user-agent header is used by the HTTP server to determine how to format some HTTP pages so they render better on the client for different web browsers and form factors (mobile phones, for example). HTTP服务器使用用户代理标头来确定如何格式化某些HTTP页面,以便它们在客户端上针对不同的Web浏览器和形状因子(例如,移动电话)更好地呈现。 Some HTTP servers return an error if no user-agent header is present on the client request. 如果客户端请求上不存在用户代理标头,则某些HTTP服务器将返回错误。 We need to add a user-agent header to avoid these errors using classes in the Windows.Web.Http.Headers namespace. 我们需要使用Windows.Web.Http.Headers命名空间中的类来添加用户代理标头,以避免这些错误。 We add this header to the HttpClient.DefaultRequestHeaders property. 我们将此标头添加到HttpClient.DefaultRequestHeaders属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM