【问题标题】:Problem with webclient: Expectation failed?webclient 的问题:期望失败?
【发布时间】:2010-06-03 23:57:20
【问题描述】:

我有一个自定义的 Http 处理程序,它操纵 HTTP POST 和 GET。我让项目在单独的隔离服务器上运行,现在需要将其投入生产......

using (var client = new WebClient())
                {
                    client.Credentials = CredentialCache.DefaultCredentials;
                    client.UploadFile("serverlocation:port", fileToUpload);
                }

由于某些原因,现在使用 client.UploadFile("", file); 即强制 HTTP POST

System.Net.WebException: The remote server returned an error: (417) Expectation failed.

   at System.Net.WebClient.UploadFile(Uri address, String method, String fileName)

这可能是什么?我知道代码有效,那还有什么?也许服务器阻止了 HTTP POST 请求?

我已经尝试添加:

ServicePointManager.Expect100Continue = false;

但没有成功,虽然我不能 100% 确定这段代码应该在哪里,我假设在我使用 WebClient 之前


编辑 0:

我只有read以下内容:

由于存在较旧的实现,该协议允许 客户可能会发送“期望:100- 继续”而没有收到 417(预期失败)状态 或 100(继续)状态。因此,当客户端发送此 头字段到源服务器(可能通过代理),它从中 从未见过 100(继续)状态,客户端不应该等待 在发送请求正文之前无限期。

我相信这个请求是通过代理进行的,这可能与问题有关。

编辑 1:

相信这个问题必须与 100-continue 相关,因为使用 fiddler 来查看我的应用程序使用 WebClient.UploadFile 发送的确切内容表明:

POST http://XXX.XXX.XXX.XXX:8091/file.myhandledextension HTTP/1.1
Content-Type: multipart/form-data; boundary=---------------------8ccd1eb03f78bc2
Host: XXX.XXX.XXX.XXX:8091
Content-Length: 4492
Expect: 100-continue

尽管在 using 语句之前放置了该行:ServicePointManager.Expect100Continue = false;。我不认为这条线真的有效。

【问题讨论】:

    标签: visual-studio httphandler webclient system.net.webexception


    【解决方案1】:

    我最终通过将 ServicePointManager.Expect100Continue = false; 放入调用 WebClient 类的构造函数中解决了这个问题。

    然后我使用Fiddler 检查 POST 请求以确保 Expect: 100-continue 不再在请求中。

    【讨论】:

      猜你喜欢
      • 2014-05-13
      • 1970-01-01
      • 2015-06-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-28
      相关资源
      最近更新 更多