2013-10-01 76 views
0

我不得不多次調用web服務(在循環中)問題是我的代碼總是返回空對象(圖片描述),並且當我單獨測試它時(環路外)它正常工作時,它不能正常運行多個HttpWebRequest循環?

這裏是我的代碼部分

HttpWebRequest httpReq = (HttpWebRequest)HttpWebRequest.Create(new Uri(imageCollection[i].ImageTag)); 

      httpReq.BeginGetResponse(new AsyncCallback((iar) => 
      { 
       try 
       { 
        string strResponse = ""; 
        var response = (HttpWebResponse)((HttpWebRequest)iar.AsyncState).EndGetResponse(iar); 

        Stream stream = response.GetResponseStream(); 
        StreamReader reader = new StreamReader(stream); 
        strResponse = reader.ReadToEnd(); 

        HtmlDocument htmlDocument = new HtmlDocument(); 
        htmlDocument.OptionFixNestedTags = true; 
        htmlDocument.LoadHtml(strResponse); 
        HtmlAgilityPack.HtmlNode titleNode = htmlDocument.DocumentNode.SelectSingleNode("//meta[@property='og:description']"); 

        if (titleNode != null) 
        { 
         string desc = titleNode.GetAttributeValue("content", ""); 
         imageCollection[i].ImageDescription = desc; 
        } 
       } 
       catch (Exception ex) 
       { 
        throw ex; 
       } 

      }), httpReq); 

      httpReq.Abort(); 

回答

0

,我從堆棧溢出的另一篇文章中修改了答案,以適應這裏我的解決方案 Getting the Response of a Asynchronous HttpWebRequest

我做了所謂的請求我的邏輯轉換爲新的特定類異步並等待在這裏它是

 public Task<string> MakeAsyncRequest() 
     { 
      HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL); 

      Task<WebResponse> task = Task.Factory.FromAsync(
       request.BeginGetResponse, 
       asyncResult => request.EndGetResponse(asyncResult), 
       (object)null); 

      return task.ContinueWith(t => ReadStreamFromResponse(t.Result)); 
     } 

     private string ReadStreamFromResponse(WebResponse response) 
     { 
      string desc = ""; 
      try 
      { 
      using (Stream responseStream = response.GetResponseStream()) 
       using (StreamReader sr = new StreamReader(responseStream)) 
       { 
        //Need to return this response 
        string strContent = sr.ReadToEnd(); 
        HtmlDocument htmlDocument = new HtmlDocument(); 
        htmlDocument.OptionFixNestedTags = true; 
        htmlDocument.LoadHtml(strContent); 
        HtmlAgilityPack.HtmlNode titleNode = htmlDocument.DocumentNode.SelectSingleNode("//meta[@property='og:description']"); 

        if (titleNode != null) 
        { 
         desc = titleNode.GetAttributeValue("content", ""); 

        } 
        imageDesc = desc; 
        return desc; 
       } 
      } 
      catch (Exception ex) 
      { return desc; } 
     } 

     public string imageDesc { get; private set; } 

    } 

然後,我提出請求的隊列

 queueWebRequest = new Queue<Request>(); 

     for (int i = 0; i < imageCollection.Count; i++) 
     { 
      queueWebRequest.Enqueue(new Request(imageCollection[i].ImageTag)); 

     } 

     for (int i = 0; i < imageCollection.Count; i++) 
     { 
      if (queueWebRequest.Count > 0) 
      { 
       Request currentRequest = queueWebRequest.Dequeue(); 
       await currentRequest.MakeAsyncRequest(); 
       imageCollection[i].ImageDescription = currentRequest.imageDesc; 

      } 
      else 
       break; 
     }