2012-02-27 97 views
2

我有一個在.NETCF 3.5設備上的C#上運行的客戶端應用程序發佈到位於遠程的Java servlet。在第三次HTTP POST期間,我收到了「請求超時」請求到同一個servlet。例如,如果servlet管理登錄到我們的Java服務器,則來自客戶端的前兩次登錄嘗試將通過(相同的客戶端設備),當我嘗試第三次登錄時,它將以「Request timed out」異常的形式返回服務器。我已經注意到這種情況總是發生,我無法弄清楚這個問題。我發現默認情況下C#發送HTTP頭中的Request 100 continue,因此我使用ServicePointManager將請求100設置爲false,但無濟於事。當將兩個HttpWebRequests從C#over .NET壓縮到同一個Java服務器時出現「請求超時」

這裏是引發此錯誤代碼:

  serverUrl = url; 
      string responseFromServer = ""; 
      try 
      { 
       System.Net.ServicePointManager.Expect100Continue = false; 
       int tmp = ServicePointManager.DefaultConnectionLimit; 
       // Create a request using a URL that can receive a post. 
       request = (HttpWebRequest)WebRequest.Create(url); 
       // Set the Method property of the request to POST. 
       request.Method = "POST"; 
       // Create POST data and convert it to a byte array.    
       byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(url); 
       // Set the ContentType property of the WebRequest. 
       request.ContentType = "application/x-www-form-urlencoded";     
       // Set the ContentLength property of the WebRequest. 
       request.ContentLength = byteArray.Length; 
       request.Timeout = (50 * 100); 
       request.Proxy = System.Net.GlobalProxySelection.GetEmptyWebProxy(); 
       // Get the request stream. 
       Stream dataStream = request.GetRequestStream(); 
       // Write the data to the request stream. 
       dataStream.Write(byteArray, 0, byteArray.Length); 
       // Close the Stream object. 
       dataStream.Close(); 
       // Get the response. 
       response = (HttpWebResponse)request.GetResponse(); 
       // Get the stream containing content returned by the server. 
       dataStream = response.GetResponseStream(); 
       // Open the stream using a StreamReader for easy access. 
       StreamReader reader = new StreamReader(dataStream); 
       // Read the content. 
       responseFromServer = reader.ReadToEnd(); 
       // Clean up the streams. 
       reader.Close(); 
       dataStream.Close(); 
       response.Close(); 

       return responseFromServer; 
      } 
      catch (Exception WebExp) 
      { 
       Logging.Instance.Log(Logging.Levels.Error, "Error in DoPost while retrieving : "+url+ " " + WebExp.Message.ToString()); 
       Logging.Instance.Log(Logging.Levels.Error, WebExp.StackTrace.ToString()); 
       throw WebExp; 
      } 

任何幫助將非常感激。謝謝!

+0

我只是罵我的登錄/註銷應用,看看它的工作,當我在第二次登錄失敗(因此第三HTTP POST),這導致我調查服務器上的註銷機制是否損壞或客戶端上的POST。這就是爲什麼我多次登錄,我知道它沒什麼意義,但它只是一種集中測試,以查看我的POST是否被破壞。 – Rishi 2012-02-27 21:17:39

回答

3

此行爲是由於有關WebResponse的錯誤異常處理。你一直要處理這個迴應並關閉它。否則,HTTP Webrequest的第三次嘗試將失敗,並由WinCE限制超時。

下面的源代碼將是安全的:

HttpWebResponse response = null; 
try 
{ 
    //... 
    response = (HttpWebResponse)request.GetResponse(); 
    //... 
} 
catch(Exception e) 
{ 
    // logging, etc. 
    throw e; 
} 
finally 
{ 
    if(response!=null) 
    { 
     response.Close(); 
    } 
} 

+0

很高興知道!你可以與WebRequests和Responses分享你的WinCE的侷限性嗎? – Rishi 2012-07-31 21:12:16

相關問題