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

是什么导致了这个异常?


当前回答

对于Powershell来说就是这样

[System.Net.ServicePointManager]::Expect100Continue = $false

其他回答

网络。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.

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

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

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

如果是这样,这一行:

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

不正确。

你需要两行代码:

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

编辑:

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

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

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

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

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

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