我已经阅读了以下两篇文章,并尝试实现了同样的方法。
我的代码是这样的,超时发生在这里
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Method = "POST";
wr.KeepAlive = false;
wr.CookieContainer = cookieJar;
wr.Proxy = null;
wr.ServicePoint.ConnectionLeaseTimeout = 5000;
Stream rs = wr.GetRequestStream(); // -> Time out error occurs hereMy code using that as a sample
HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(url);
wr.ContentType = "multipart/form-data; boundary=" + boundary;
wr.Method = "POST";
wr.KeepAlive = false;
wr.CookieContainer = cookieJar;
wr.Timeout = 5000;
wr.Proxy = null;
wr.ServicePoint.ConnectionLeaseTimeout = 5000;
wr.ServicePoint.MaxIdleTime = 5000;
Stream rs = wr.GetRequestStream(); //-->Time out error任何线索都会有帮助。有时,我可以通过一个请求,所有其他请求都会失败,或者一切都会失败。我张贴到HTTPS网站。使用Fiddler运行时没有问题
更新1:我尝试了zbugs的想法,但结果还是出现了同样的问题。第一个请求通过,随后的请求失败。我正在关闭所有响应流,并在我的请求对象上调用abort。
发布于 2012-05-19 04:38:43
您需要设置此设置。
const int maxIdleTimeout = 5000;
ServicePointManager.MaxServicePointIdleTime = maxIdleTimeout;如果您在任何给定时间都有多个客户端发出请求,
const int maxConnections = 100; //or more/less
ServicePointManager.DefaultConnectionLimit = maxConnections;http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.maxservicepointidletime.aspx
发布于 2012-05-19 04:45:12
您应该释放所有实现IDisposable的资源。不发布它们可能会导致您正在试验的那种问题。
这里有一个示例,它并不完全符合您的代码,但它将帮助您理解我的意思
var request = WebRequest.Create(url);
request.ContentType = "application/json; charset=utf-8";
using (var response = request.GetResponse())
{
using (var sr = new StreamReader(response.GetResponseStream()))
{
...
}
}发布于 2012-05-21 23:16:56
在我的一次请求调用中,我未能调用
Request.Abort()这似乎解决了我的问题以及来自zbugs的建议。
https://stackoverflow.com/questions/10659308
复制相似问题