2011-09-19 70 views
10

我想發送多個WebRequest。我使用了Parallel.For循環來做到這一點,但循環運行一次,第二次在獲得響應時出現錯誤。並行發送多個WebRequest。對於

錯誤:

The operation has timed out

代碼:

Parallel.For(0, 10, delegate(int i) { 
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(
     new Uri("http://www.mysite.com/service")); 

    string dataToSend = "Data"; 
    byte[] buffer = System.Text.Encoding.GetEncoding(1252). 
     GetBytes(dataToSend); 
    request.Method = "POST"; 
    request.ContentType = "application/x-www-form-urlencoded"; 
    request.ContentLength = buffer.Length; 

    request.Host = "www.mysite.com"; 

    Stream requestStream = request.GetRequestStream(); 
    requestStream.Write(buffer, 0, buffer.Length); 
    requestStream.Close(); 

    HttpWebResponse response = (HttpWebResponse)request.GetResponse(); 
}); 
+2

它給你一個錯誤信息?真?你想問一些特別的問題嗎? –

+0

而錯誤是...? – Icarus

+0

什麼是例外,它在哪裏發生?你確定服務器實際上沒有超時嗎? –

回答

10

最有可能的問題是,你需要調用response.Close()大功告成處理響應之後。