我正在使用WPF .net 4.0應用程序。我有一個搜索欄。對於每個搜索令牌,我需要對8個單獨的URL執行8次http請求以獲取搜索結果。一旦用戶停止在搜索欄中輸入內容,我會在400毫秒後向服務器發送8個請求。搜索6到7個搜索令牌結果非常好。但之後突然HttpWebRequest停止工作靜默。沒有發生異常,沒有收到任何迴應。我正在使用Windows 7,我也禁用了防火牆。我不知道後續http請求丟失的位置。HttpWebRequest突然停止工作,幾個請求後沒有收到響應
任何人都可以讓我看看燈光來解決這個問題嗎?
下面是我爲HttpWebRequest調用的代碼。
public static void SendReq(string url)
{
// Create a new HttpWebRequest object.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Proxy = new WebProxy("192.168.1.1", 8000);
// Set the Method property to 'POST' to post data to the URI.
request.Method = "POST";
// start the asynchronous operation
request.BeginGetRequestStream(new AsyncCallback(GetRequestStreamCallback), request);
}
private static void GetRequestStreamCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
Stream postStream = request.EndGetRequestStream(asynchronousResult);
string postData = this.PostData;
// Convert the string into a byte array.
byte[] byteArray = Encoding.UTF8.GetBytes(postData);
// Write to the request stream.
postStream.Write(byteArray, 0, byteArray.Length);
postStream.Close();
// Start the asynchronous operation to get the response
request.BeginGetResponse(new AsyncCallback(GetResponseCallback), request);
}
private static void GetResponseCallback(IAsyncResult asynchronousResult)
{
HttpWebRequest request = (HttpWebRequest)asynchronousResult.AsyncState;
// End the operation
using(HttpWebResponse response = (HttpWebResponse)request.EndGetResponse(asynchronousResult))
{
using(Stream streamResponse = response.GetResponseStream())
{
using(StreamReader streamRead = new StreamReader(streamResponse))
{
string responseString = streamRead.ReadToEnd();
Debug.WriteLine(responseString);
}
}
}
}
你有多確定沒有其他請求引發異常?您的'Close'調用不在'using'語句中,這意味着如果有例外,您將會打開響應連接,這可能會導致後續請求死鎖... –
不會引發異常。應用程序完全無聲。沒有收到任何響應GetResponseCallback在6到7次請求後從未被調用。我已經更新了代碼。 *增加*使用*但仍然是相同的問題。 – Somnath
你有沒有在網絡層面看到Wireshark這樣的事情? –