当我尝试POST到一个URL时,会导致以下异常:

远程服务器返回错误: (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没有区别。

是什么导致了这个异常?


当前回答

检查网络连接是否重定向。

我有这个问题时,在错误的wifi和任何网络请求重定向到一个公司登录页面。

其他回答

如果你正在使用“HttpClient”,并且你不想使用全局配置来影响你可以使用的所有程序:

 HttpClientHandler httpClientHandler = new HttpClientHandler();
 httpClient.DefaultRequestHeaders.ExpectContinue = false;

如果你正在使用“WebClient”,我认为你可以尝试删除这个头通过调用:

 var client = new WebClient();
 client.Headers.Remove(HttpRequestHeader.Expect);

网络。config方法适用于InfoPath表单服务调用IntApp启用web服务的规则。

  <system.net>
    <defaultProxy />
    <settings> <!-- 20130323 bchauvin -->
        <servicePointManager expect100Continue="false" />
    </settings>
  </system.net>

System.Net.HttpWebRequest向每个请求添加报头“HTTP报头”Expect: 100-Continue”,除非你通过将这个静态属性设置为false显式地要求它不要这样做:

System.Net.ServicePointManager.Expect100Continue = false;

有些服务器会阻塞这个报头,并返回您所看到的417错误。

试试吧。

Solution from proxy side, I faced some problems in the SSL handshake process and I had to force my proxy server to send requests using HTTP/1.0 to solve the problem by setting this argument in the httpd.conf SetEnv force-proxy-request-1.0 1 SetEnv proxy-nokeepalive 1 after that I faced the 417 error as my clients application was using HTTP/1.1 and the proxy was forced to use HTTP/1.0, the problem was solved by setting this parameter in the httpd.conf on the proxy side RequestHeader unset Expect early without the need to change anything in the client side, hope this helps.

检查网络连接是否重定向。

我有这个问题时,在错误的wifi和任何网络请求重定向到一个公司登录页面。