【问题标题】:The request was aborted: The operation has timed out. on HttpWebResponse.Request.GetResponse()请求已中止:操作已超时。在 HttpWebResponse.Request.GetResponse()
【发布时间】:2017-07-13 12:38:27
【问题描述】:

我总共有 12 个连续请求,在第 6 个 请求中我收到了这个错误。 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>

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

【问题讨论】:

    标签: c# asp.net iis fileserver


    【解决方案1】:

    这是我从 18 秒到 5 毫秒的最终代码。我还在代码末尾添加了垃圾收集。

        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;
        }
    

    【讨论】:

      【解决方案2】:

      尝试添加:

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

      还有,

      添加这些:

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

      【讨论】:

      • 根据我的异常日志,错误还是出现了The request was aborted: The operation has timed out.,响应时间还是18秒。
      • 成功了,我将 50000 编辑为 500,因此超时将是 5 毫秒,而不是最大 50 秒。
      • @JAlcantara 很高兴我能帮上忙。来自以色列的和平与爱。
      猜你喜欢
      • 2019-02-14
      • 2013-03-08
      • 1970-01-01
      • 1970-01-01
      • 2012-09-21
      • 2011-08-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多