2012-09-20 64 views
5

的下面一段代碼是給予 錯誤消息:「該操作已超時」 錯誤水稻源:在System.Net.httpWebRequest.GetResponse()System.Net.WebRequest - 超時錯誤

這方法正在調用URL並獲取響應對象。

注:這是我end..but所有工作正常,當我相同的代碼發送到production..it顯示時間oout錯誤

public GetUpdatedInventoryUnitValues(Vehicle aeVehicle) 
{ 
      WebRequest oWebRequest = null; 
      StringBuilder oStringBuilder = null; 
      StreamReader oStreamReader = null; 
      dcDealerDetails = new Dictionary<string, string>(); 

      MSRP = string.Empty; 
      NetPrice = string.Empty; 
      string strLine = string.Empty; 
      string strURL = GetUpdatedInventoryUnitValues.GetFormattedURL(aeVehicle); 

      try 
      { 
       /* Open the requested URL */ 
       oWebRequest = WebRequest.Create(strURL); 
       oWebRequest.Method = "GET"; 
       oWebRequest.ContentType = "application/xml"; 
       /* Get the stream from the returned web response */ 
       oStreamReader = new StreamReader(oWebRequest.GetResponse().GetResponseStream()); 
       /* Get the stream from the returned web response */ 
       oStringBuilder = new StringBuilder(); 
       /* Read the stream a line at a time and place each one into the stringbuilder */ 
       while ((strLine = oStreamReader.ReadLine()) != null) 
       { 
        /* Ignore blank lines */ 
        if (strLine.Length > 0) 
         oStringBuilder.Append(strLine); 
       } 

       string[] tempArray = null; 
       string[] tempNextArray = null; 
       //Split string by semicolon as a separater 
       tempArray = Data.SplitString(oStringBuilder.ToString(), new char[] { ';' }); 

       if (tempArray != null) 
       { 
        foreach (string invUnits in tempArray) 
        { 
         //Split string by '=' as a separater 
         tempNextArray = Data.SplitString(invUnits, new char[] { '=' }); 

         if (tempNextArray != null && tempNextArray.Length == 2) 
         { 
          switch (tempNextArray[0].ToLower()) 
          { 
           //case "msrp": 
           // MSRP = Data.RemoveDoubleCode(tempNextArray[1]); 
           // break; 
           case "netprice": 
            NetPrice = Data.RemoveDoubleCode(tempNextArray[1]); 
            break; 
          } 
         } 
        } 
       } 
      } 
      catch (Exception ex) 
      { 
       ErrorLog.ErrorMessage = ErrorLog.Separator; 
       ErrorLog.ErrorMessage = "Exception during posting data to another application ."; 
       ErrorLog.ErrorMessage = "ERROR MESSAGE : " + ex.Message; 
       ErrorLog.ErrorMessage = "ERROR SOURCE: " + ex.StackTrace.ToString(); 

      } 
      finally 
      { 
       if (oStreamReader != null) 
       { 
        oStreamReader.Close(); 
       } 
       if (oWebRequest != null) 
       { 
        oWebRequest = null; 
       } 
      } 
     } 

請建議我在做什麼錯誤或丟失?

+0

你確定你的要求是好的,它不會超時出來的代碼?我的意思是你是否試圖直接在瀏覽器中啓動它? – 2012-09-20 12:57:30

+0

這是所有工作正常在我的end..but當我發送相同的代碼生產..它顯示時間oout錯誤 –

+0

您的開發平臺和您的生產平臺之間有什麼區別?有沒有防火牆或類似的東西可以阻止你的請求? – 2012-09-20 13:01:59

回答

17

您是否也許發現第一對請求是好的,然後然後他們開始超時?如果是這樣,我懷疑這是問題:

oStreamReader = new StreamReader(oWebRequest.GetResponse().GetResponseStream()); 

您正在提取響應,但從不處理它。您應該使用:

​​

事實上,你可以擺脫finally塊的全部,如果你使用using陳述貫穿始終。

另外,這是一個相當長的方法 - 77行! - 更糟的是,它看起來像它實際上是一個構造函數:

  • 嘗試它分割成更小,更容易理解,更容易測試塊
  • 儘量避免做了很多工作,在構造函數中
+0

..這是工作正常,我的250個呼叫在循環..但在生產中,它幾乎全部是超時...在代碼部分..我應該這樣做.. –

+0

@RatanSharma:你從生產中獲取什麼網址?它可能真的只是超時嗎? –

+0

上面的函數是從生產站點中運行的工具...該函數正在調用一個URL來給出一些響應。當我在我的系統中運行該工具時,這是工作正常,但在生產服務器時我正在運行相同它正在調整.. –

1

我personnally使用此代碼爲我的程序之一,它完美的作品:

WebRequest webRequest = WebRequest.Create(requestUri); 
    webRequest.Credentials = new NetworkCredential(login, password); 
    WebResponse webResponse = webRequest.GetResponse(); 
    Stream response = webResponse.GetResponseStream(); 
    StreamReader reader = new StreamReader(response); 

所以我認爲它不是來自你的代碼,但是從你的生產平臺。

+4

如果你不處理迴應,那麼它不能完美運作 - 你只是幸運。 –

+0

通過在使用結束後使用您的發佈代碼來處理響應? – 2012-09-20 13:27:40

+1

是的。你應該使用'using'語句來實現'IDisposable',以確保清理。 –

2

只是分享經驗。

我得到了同樣的錯誤「操作超時」。

我已經嘗試使用WebClient和WebRequest(設置超時也)但仍然出現錯誤。

原因是我沒有處理響應。

所以我用上面提到:

using (var response = oWebRequest.GetResponse()) 

{ 
    ... 
} 

它解決了這個問題...