请求已中止:操作已超时。在 HttpWebResponse.Request.GetResponse()

The request was aborted: The operation has timed out. on HttpWebResponse.Request.GetResponse()

我总共有 12 个连续请求,在 6th 个请求中我遇到了这个错误。 The request was aborted: The operation has timed out.(预计时间为 18 秒)。在 6th 请求之后,所有剩余的请求再次快速。为什么第 6 个请求与所有其他请求相比太慢?

这是我的代码:

 public bool CreateFolder(string _strDirectory)
    {
        bool result = true;
        System.Net.HttpWebRequest Request;
        CredentialCache MyCredentialCache;
        MyCredentialCache = new System.Net.CredentialCache();
        MyCredentialCache.Add(new System.Uri(_strDirectory), "NTLM", new System.Net.NetworkCredential(UserName, Password));
        Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(_strDirectory);
        Request.Credentials = MyCredentialCache;
        Request.Method = "MKCOL";
        Request.Proxy = null;
        ServicePointManager.Expect100Continue = false;
        try
        {
            using (var response = (System.Net.HttpWebResponse)Request.GetResponse())
            {
            }
        }
        catch (Exception)
        {
            throw;
        }
        Request.Abort();
        return result;
    }

我也添加到我的Web.config这行代码:

<system.net>
<connectionManagement>
  <clear/>
  <add address="*" maxconnection="1000000" />
</connectionManagement>

有什么想法吗?非常感谢!

尝试添加:

<system.web>
    <httpRuntime executionTimeout="90000" maxRequestLength="1048576" />
    ....

此外,

添加这些:

request.KeepAlive = false;
request.Timeout = 50000;
request.ServicePoint.ConnectionLeaseTimeout = 50000;
request.ServicePoint.MaxIdleTime = 50000;

这是我从 18 秒到 5 毫秒的最终代码。我还在代码末尾添加了 Garbage Collect。

    public bool CreateFolder(string _strDirectory)
    {
        bool result = true;

        System.Net.HttpWebRequest Request;
        CredentialCache MyCredentialCache;
        MyCredentialCache = new System.Net.CredentialCache();
        MyCredentialCache.Add(new System.Uri(_strDirectory), "NTLM", new System.Net.NetworkCredential(UserName, Password));
        Request = (System.Net.HttpWebRequest)HttpWebRequest.Create(_strDirectory);
        Request.Credentials = MyCredentialCache;
        Request.Method = "MKCOL";
        Request.KeepAlive = false;
        Request.Timeout = 500;
        Request.Proxy = null;

        Request.ServicePoint.ConnectionLeaseTimeout = 500;
        Request.ServicePoint.MaxIdleTime = 500;

        try
        {
            using (var response = (System.Net.HttpWebResponse)Request.GetResponse())
            {
            }
        }
        catch (Exception)
        {
        }
        Request.Abort();
        GC.Collect();
        return result;
    }