本文介绍了HTTP POST 返回错误:417“期望失败".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我尝试 POST 到一个 URL 时,它会导致以下异常:
When I try to POST to a URL it results in the following exception:
远程服务器返回错误:(417) 期望失败.
这是一个示例代码:
var client = new WebClient();
var postData = new NameValueCollection();
postData.Add("postParamName", "postParamValue");
byte[] responseBytes = client.UploadValues("http://...", postData);
string response = Encoding.UTF8.GetString(responseBytes); // (417) Expectation Failed.
使用 HttpWebRequest/HttpWebResponse
对或 HttpClient
没有区别.
Using an HttpWebRequest/HttpWebResponse
pair or an HttpClient
doesn't make a difference.
是什么导致了这个异常?
What's causing this exception?
推荐答案
System.Net.HttpWebRequest 向每个请求添加标头 'HTTP header "Expect: 100-Continue"' 除非您通过设置 这个静态属性为假:
System.Net.HttpWebRequest adds the header 'HTTP header "Expect: 100-Continue"' to every request unless you explicitly ask it not to by setting this static property to false:
System.Net.ServicePointManager.Expect100Continue = false;
某些服务器阻塞在该标头上并发回您看到的 417 错误.
Some servers choke on that header and send back the 417 error you're seeing.
试一试.
这篇关于HTTP POST 返回错误:417“期望失败".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!