2

我已阅读以下 2 篇文章并尝试实施相同的文章。

我的代码是这个,超时发生在这里

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 here

我读的文章

My 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

任何线索都会有所帮助。有时我可以通过 1 个请求,而所有其他请求都失败或一切都失败了。我正在发布到 HTTPS 站点。使用 Fiddler 运行时没有问题

更新 1:我尝试遵循 zbugs 的想法,但这导致了同样的问题。第一个请求通过后续请求失败。我正在关闭所有响应流以及在我的请求对象上调用 abort。

4

3 回答 3

4

你需要设置这个。

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-18T20:38:42.583 回答
1

您应该释放所有实现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-18T20:45:12.130 回答
0

在我的一个请求电话中,我没有打电话

Request.Abort()

这似乎已经解决了我的问题以及 zbugs 的建议

于 2012-05-21T15:16:56.047 回答