我有一個在.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;
}
任何幫助將非常感激。謝謝!
我只是罵我的登錄/註銷應用,看看它的工作,當我在第二次登錄失敗(因此第三HTTP POST),這導致我調查服務器上的註銷機制是否損壞或客戶端上的POST。這就是爲什麼我多次登錄,我知道它沒什麼意義,但它只是一種集中測試,以查看我的POST是否被破壞。 – Rishi 2012-02-27 21:17:39