gpt4 book ai didi

c# - HttpWebRequest 超时

转载 作者:行者123 更新时间:2023-12-05 01:48:34 29 4
gpt4 key购买 nike

我已阅读以下 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

Article I read

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 的想法,但这导致了同样的问题。第一个请求通过,随后的请求失败。我正在关闭所有响应流并在我的请求对象上调用中止。

最佳答案

你需要设置这个。

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

关于c# - HttpWebRequest 超时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10659308/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com