HttpWebRequest 超时

use*_*264 3 c# timeout httpwebrequest

我已阅读以下两篇文章并尝试实现相同的内容。

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

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
Run Code Online (Sandbox Code Playgroud)

我读过的文章

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
Run Code Online (Sandbox Code Playgroud)

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

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

小智 5

你需要设置这个。

const int maxIdleTimeout = 5000;
ServicePointManager.MaxServicePointIdleTime = maxIdleTimeout;
Run Code Online (Sandbox Code Playgroud)

如果您在任何给定时间有多个客户提出请求,

const int maxConnections = 100; //or more/less
ServicePointManager.DefaultConnectionLimit = maxConnections;
Run Code Online (Sandbox Code Playgroud)

http://msdn.microsoft.com/en-us/library/system.net.servicepointmanager.maxservicepointidletime.aspx