2011-04-20 49 views
1

我創建了一個WebRequest以將一些HTML內容發佈到另一個Web服務器。當我使用正常的文本內容時,它可以正常工作,但是當我發佈HTML內容時,在調用GetResponse()時出現超時錯誤。調用GetResponse()時超時異常

WebResponse response = request.GetResponse() 

我怎樣才能找出問題? 我試着爲WebException添加一個錯誤處理程序,但我無法捕捉到這個異常。

請求代碼:

byte[] byteArray = Encoding.UTF8.GetBytes(sPostData); 

// Create a request using a URL that can receive a post. 
WebRequest request = WebRequest.Create(httpUri); 
// Set the 'Timeout' property in Milliseconds. 
//request.Timeout = 20000; 
// Set the ContentType property of the WebRequest. 
request.ContentType = contentType; 
// Set the Method property of the request to POST. 
request.Method = postMethod; 
// Set the ContentLength property of the WebRequest. 
request.ContentLength = byteArray.Length; 
// Get the request stream. 
using (Stream PostStream = request.GetRequestStream()) 
{ 
    PostStream.Write(byteArray, 0, byteArray.Length); 
} 
// Get the response. 
using (WebResponse response = request.GetResponse()) 
{ 
    // Get the stream containing content returned by the server. 
    using (Stream responseStream = response.GetResponseStream()) 
    { 
     using (StreamReader reader = new StreamReader(responseStream)) 
     { 
      string responseFromServer = reader.ReadToEnd(); 

      result = responseFromServer; 
     } 
    } 
} 
+0

您是否也可以顯示請求代碼? – BrokenGlass 2011-04-20 01:34:06

+0

請參閱請求代碼已添加到問題 – Roshe 2011-04-20 02:43:26

+0

任何想法? – Roshe 2011-04-26 04:34:02

回答

0

你應該看看使用Fiddler或其他相當的應用程序的網絡流量。這可能會向您顯示服務器是否正在響應,或者可能是響應遲了。

您還應該查看服務器日誌或Windows事件日誌 - 服務器可能不喜歡被髮送HTML並記錄錯誤。在這種情況下,它可能只是簡單地決定你試圖破解它,它可能決定根本不回答你。

+0

感謝John的回覆。如果我修改HTML如下它的工作原理,

測試

我有一些HTML泰國語字符,會是一個問題嗎? – Roshe 2011-04-20 02:11:37

+0

你編碼正確嗎? – 2011-04-20 06:04:58

+0

我使用以下的, byte [] byteArray = Encoding.UTF8.GetBytes(sPostData); – Roshe 2011-04-20 07:03:39