当我尝试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没有区别。

是什么导致了这个异常?


当前回答

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.

其他回答

在我的情况下,这个错误似乎只有在我客户的计算机有严格的防火墙策略时才会发生,防火墙策略阻止了我的程序与web服务通信。

所以我能找到的唯一解决方案是捕捉错误并通知用户手动更改防火墙设置。

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

System.Net.ServicePointManager.Expect100Continue = false;

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

试试吧。

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

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

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.

您试图模拟的表单是否有两个字段,用户名和密码?

如果是这样,这一行:

 postData.Add("username", "password");

不正确。

你需要两行代码:

 postData.Add("username", "Moose");
postData.Add("password", "NotMoosespasswordreally");

编辑:

好吧,既然这不是问题所在,解决这个问题的一种方法是使用Fiddler或Wireshark之类的工具来查看从浏览器成功发送到web服务器的内容,然后将其与从您的代码发送的内容进行比较。如果从。net转到普通的80端口,Fiddler仍然会捕获这些流量。

表单上可能有一些web服务器期望你没有发送的其他隐藏字段。