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

时间:2017-02-23 06:48:34

标签: c# asp.net iis fileserver

我总共有 12 连续请求,并且在第6次请求中出现了此错误。 The request was aborted: The operation has timed out.(预计时间为18秒)。在第6次请求之后,所有剩余请求再次快速。为什么第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>

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

2 个答案:

答案 0 :(得分:2)

尝试添加:

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

另外,

添加以下内容:

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

答案 1 :(得分:0)

这是我的最终代码,从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;
    }