c# - HttpWebRequest 底层连接被关闭

标签 c# post httpwebrequest

我正在使用 HttpWebRequest 通过网络服务POST 字节数组图片,图片大小类似于 byte[4096]

代码:

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(wsHost);
webRequest.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip,deflate");
webRequest.Headers.Add(HttpRequestHeader.KeepAlive, "true");

我得到一个错误:

The underlying connection was closed. A connection that was expected to be kept alive was closed by the server

是服务器的问题,还是我发帖的问题?

最佳答案

可能有很多东西。否则你能连接到服务器吗?

如果是这样,请尝试关闭 Expected 100 Continue(在您进行 POST 之前)通过

System.Net.ServicePointManager.Expect100Continue = false;

According to the HTTP 1.1 protocol, when this header is sent, the form data is not sent with the initial request. Instead, this header is sent to the web server which responds with 100 (Continue) if implemented correctly. However, not all web servers handle this correctly, including the server to which I am attempting to post data.

通过http://haacked.com/archive/2004/05/15/http-web-request-expect-100-continue.aspx

如果这不起作用另一个资源:http://geekswithblogs.net/Denis/archive/2005/08/16/50365.aspx表明许多人已经通过将请求构造为 HTTP 1.0 请求来解决问题:

HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(wsHost);
webRequest.KeepAlive = false;
webRequest.ProtocolVersion=HttpVersion.Version10;

关于c# - HttpWebRequest 底层连接被关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13188058/

相关文章:

c# - 项目无法识别包含在不同程序集中的构造函数

php - Javascript POST 不起作用 - 将 Javascript 数组发送到 PHP

c# - 使用 httprequest background worker UI 卡住 1 或 2 秒

c# - 使用 BasicHttpBinding、http header 和 soap 信封的 WCF 堆栈中的数据包碎片总是由数据包边界分割?

c# - 用 Action 包围一个函数

c# - MySQL C# 连接字符串故障转移

c# - 在 epplus 的数据透视表中隐藏小计

python - 基于requests模块将curl执行的post请求转换为python请求

php - html post form action 没有专门拼出来的php文件?

java - 对每个 httpclient 请求重复使用相同的 session