2017-02-23 100 views
0

我總共有連續請求和第6請求我得到這個錯誤。 The request was aborted: The operation has timed out.(預計時間爲18秒)。並且在請求所有剩餘的請求之後,再次快速地請求第6個。爲什麼第6次請求比其他請求慢?請求已中止:操作已超時。在HttpWebResponse.Request.GetResponse()

這是我的代碼:

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> 

任何想法?非常感謝你!

回答

1

嘗試增加:

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

此外,

添加這些:

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

基於我的異常日誌,錯誤仍然發生'請求被中止:操作超時.'和響應時間仍然是18秒。 – Joseph

+0

這樣做的伎倆,我編輯50000 500,所以超時將是5毫秒不50秒作爲最大。 – Joseph

+1

@JAlcantara很高興我能幫到你。來自以色列的和平與愛。 –

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; 
    } 
相關問題