WebRequest 预期保持活动状态的连接被服务器关闭

WebRequest A connection that was expected to be kept alive was closed by the server

在脚本任务中使用 Webrequest 来使用 Rest 服务

我收到以下错误

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

我在 For Each Loop 容器中调用其余服务。 它第一次工作但没有任何后续执行。

搜索堆栈溢出我遇到了以下设置,这些设置对其他人有帮助但对我没有帮助

ServicePointManager.Expect100Continue = false;
ServicePointManager.DefaultConnectionLimit = 100;
ServicePointManager.MaxServicePointIdleTime = 5000;

我认为这是因为 KeepAlive 属性 设置为 true 但我看不到更改它的方法 it isn't a property 可以更改

你可以投

HttpWebRequest req = WebRequest.Create("https://www.server.com/api/stuff") as HttpWebRequest;

req.KeepAlive = false;
HttpWebResponse response = (HttpWebResponse)req.GetResponse();

或覆盖

internal class MyWebClient : WebClient
{
    override WebRequest GetWebRequest(Uri address)
    {
        HttpWebRequest req = (HttpWebRequest)base.GetWebRequest(address);
        req.KeepAlive = false;

        return req;
    }
}

您的 WebRequest 可以访问更多隐藏或抽象的属性!