2012-12-21 90 views
5

我正在使用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); 
      } 
     } 
    } 
} 
+1

你有多確定沒有其他請求引發異常?您的'Close'調用不在'using'語句中,這意味着如果有例外,您將會打開響應連接,這可能會導致後續請求死鎖... –

+0

不會引發異常。應用程序完全無聲。沒有收到任何響應GetResponseCallback在6到7次請求後從未被調用。我已經更新了代碼。 *增加*使用*但仍然是相同的問題。 – Somnath

+0

你有沒有在網絡層面看到Wireshark這樣的事情? –

回答

2

所有我能看到的是,在GetRequestStreamCallback你應該

postStream.Write(byteArray, 0, byteArray.Length); 

更換

postStream.Write(byteArray, 0, postData.Length); 

因爲這些長度不一定相等。

+0

謝謝克萊門斯,你說得對。剛纔我在代碼中做了更改。即使這並不能解決HttpWebRequest無聲失敗的問題。 – Somnath

0

@Somnath 我不確定你是否找到了答案,但是如果有人在Somnath和我所在的同一個問題上絆倒了這篇文章。

我們都盡我們的努力來保持記憶的清潔和清晰,但如果我們確保在關閉它之前沖洗流,我們總是會保存不明原因的問題。

替換此:

postStream.Write(byteArray, 0, byteArray.Length); 
postStream.Close(); 

這一點:

​​
+0

嗨Jdunn,剛纔我測試了你的建議,但失敗了。我衝過來關閉了所有的小溪。所以我的代碼中沒有內存泄漏。仍然沒有運氣。請注意,當我繼續在搜索框中輸入內容時,我發送的請求可能會在某些請求返回響應之前發送。我只是忽略了以前的迴應。現在我想我需要做一些取消請求的工作。你在想什麼?任何猜測..! – Somnath

-1

我跟所有的建議,所有的你提供,但是不能停止HTTP請求的無聲故障。 但我找到了解決方法。直到現在,我自己也達不到最終結論。

但我的解決方法現在運行良好,現在沒有任何失敗。

SendReq(字符串URL)功能我已經添加的代碼

System.Net.ServicePointManager.DefaultConnectionLimit = 100; // Just selected a random number for testing greater than 2 
System.Net.ServicePointManager.SetTcpKeepAlive(true, 30, 30); // 30 is based on my server i'm hitting 
System.Net.ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3; 

request.KeepAlive = true; 
4

我覺得我很晚了,但我還是想回答你的問題如下行,可能是它可以對他人有幫助。默認情況下HTTP請求是HTTP 1.1請求。而HTTP 1.1請求默認有Keep-Alive連接。所以當你向同一臺服務器發出太多的請求時.net框架只會讓x no。的請求。

你應該附近response.Close()

所有響應,你還可以指定你多少可以同時請求作出。

ServicePointManager.DefaultConnectionLimit = 20; 

請注意,您必須在第一次請求之前設置DefaultConnectionLimit。你可以找到更多的信息 here on msdn