2013-07-09 51 views
0

我有一個json字符串的列表,我嘗試循環併發布到WebService。它循環前兩個就好了,然後停在第3次迭代(這發生在第14行)。沒有錯誤或任何事情,它似乎停止。有任何想法嗎?GetRequestStream在數組中停止

private static void CacheGetDataUrl(Guid sessionGuid) 
    { 
     var tokens = Driver.GetDataPostValues(); 
     foreach (var token in tokens) 
     { 
      try 
      { 
       var buffer = Encoding.UTF8.GetBytes(token.Replace("session_identifier", sessionGuid.ToString())); 
       var request = (HttpWebRequest)WebRequest.Create(GetDataUrl); 
       request.Timeout = 1000000; 
       request.Method = "POST"; 
       request.ContentType = "application/json; charset=utf-8"; 
       request.ContentLength = buffer.Length; 
       using (var postData = request.GetRequestStream()) 
       { 
        postData.Write(buffer, 0, buffer.Length); 
        postData.Close(); 
       } 
       Console.WriteLine("Response requested for URL {0}", GetDataUrl); 
       Log.Info(String.Format("Response requested for URL {0} using parameters \r\n{1}", GetDataUrl, token)); 
      } 
      catch (Exception) 
      { 
       Console.WriteLine("Error occurred requesting response for URL {0} using parameters \r\n{1}", GetDataUrl, token); 
       Log.Error(String.Format("Error occurred requesting response for URL {0} using parameters \r\n{1}", GetDataUrl, token)); 
      } 
     } 

    } 

回答

0

使用一些新屬性更改方法,並在收到請求後中止請求,似乎解決了問題。

private static void CacheGetDataUrl(Guid sessionGuid) 
    { 
     var tokens = Driver.GetDataPostValues(); 
     foreach (var token in tokens) 
     { 
      try 
      { 
       var buffer = Encoding.UTF8.GetBytes(token.Replace("session_identifier", sessionGuid.ToString())); 
       var request = (HttpWebRequest)WebRequest.Create(GetDataUrl); 
       request.KeepAlive = false; 
       request.Timeout = System.Threading.Timeout.Infinite; 
       request.ProtocolVersion = HttpVersion.Version10; 
       request.Method = "POST"; 
       request.ContentType = "application/json; charset=utf-8"; 
       request.ContentLength = buffer.Length; 
       request.ServicePoint.ConnectionLimit = 250; 
       using (var postData = request.GetRequestStream()) 
       { 
        postData.Write(buffer, 0, buffer.Length); 
        postData.Close(); 
       } 

       request.Abort(); 

       Console.WriteLine("Response requested for URL {0}", GetDataUrl); 
       Log.Info(String.Format("Response requested for URL {0} using parameters \r\n{1}", GetDataUrl, token)); 
      } 
      catch (Exception) 
      { 
       Console.WriteLine("Error occurred requesting response for URL {0} using parameters \r\n{1}", GetDataUrl, token); 
       Log.Error(String.Format("Error occurred requesting response for URL {0} using parameters \r\n{1}", GetDataUrl, token)); 
      } 
     } 
    }