2013-08-16 42 views
0

我想創建一個程序,可以排序與任何指定的谷歌搜索相關聯的結果數量。我需要一張大桌子,所以我想要使用一個循環。每次我嘗試它時,由於「System.Windows.Markup.XamlParseException」,調試器崩潰。試圖循環HttpWebRequest沒有成功

public long resultStat(string a) 
    { 
     var req = (HttpWebRequest)WebRequest.Create("https://www.google.ca/search?hl=fr&output=search&sclient=psy-ab&q=a" + a + "&btnK="); 
     using (req as IDisposable) 
     { 
      WebResponse rep = req.GetResponse(); 
      Stream str = rep.GetResponseStream(); 
      StreamReader rdr = new StreamReader(str); 
      string res = rdr.ReadToEnd(); 
      rdr.Close(); 
      //This is my code to get the number results (it works perfectly) 
      int index = res.IndexOf(">Environ"); 
      int cond = 0; 
      string final = ""; 
      try 
      { 
       while (res[++index] != '<') 
       { 
        if (cond-- == 0 && res[index] != '&') 
        { final += res[index]; cond = 0; } 
        else if (res[index] == '&') cond = 5; 
       } 
      } 
      catch { return 0; } 
      string temp = ""; 
      foreach (char i in final) if (i < 48 && i > 58) temp += i; 
      return Int64.Parse(temp); 
     } 
    } 

這整個方法是在主只需使用一個for循環,例如:

public void main() 
{ 
    //Other code 
    for (int i = 0; i < 3; i++) resultStat(i.ToString()); // For example 
    //Other code 
} 

我知道這是問題,因爲只要我評論的循環,或將其降低到一個代表,沒有任何問題。我試過了:

HttpWebRequest()。Abort(); HttpWebRequest()。KeepAlive = false;

它沒有工作

+0

你可以嘗試用'long l = 0替換'return Int64.Parse(temp);'; Int64.TryParse(temp,out l);返回l;'? – I4V

+0

它的工作,但後來我試圖將每個結果存儲到一個列表中,並再次墜毀...但謝謝你的提示,我會尋找其他類似的錯誤 – Hadron

回答

0

我不認爲你正在做的客場是做這種正確的方法。我可以告訴你的簡單的一個就是使用Lib curl c#。您可以發送一組網址並以數組形式獲取響應。這對你在這裏所需要的是完美的。以下是多任務處理本身的示例代碼。您只需發送到網址。

public class MultiHttp 
{ 
    public static string UserAgent = "Mozilla 5.0"; 
    public static string Header = "Content-Type: application/x-www-form-urlencoded; charset=UTF-8"; 
    private static string[] Result; 

    public static string[] MultiPost(string[] Url, string post, int timeOut) 
    { 
     Result = new string[post.Length];   
     try 
     { 
      Curl.GlobalInit((int)CURLinitFlag.CURL_GLOBAL_ALL); 

      Easy.WriteFunction wf = new Easy.WriteFunction(OnWriteData); 
      //Easy.HeaderFunction hf = new Easy.HeaderFunction(OnHeaderData); 

      Easy[] easy = new Easy[Url.Length]; 
      Multi multi = new Multi(); 
      for (int i = 0; i < Url.Length; i++) 
      { 
       if (Url[i] != null) 
       { 
        easy[i] = new Easy(); 
        easy[i].SetOpt(CURLoption.CURLOPT_URL, Url[i]); 
        easy[i].SetOpt(CURLoption.CURLOPT_WRITEFUNCTION, wf); 
        easy[i].SetOpt(CURLoption.CURLOPT_WRITEDATA, i); 
        //easy[i].SetOpt(CURLoption.CURLOPT_HEADERFUNCTION, hf); 
        //easy[i].SetOpt(CURLoption.CURLOPT_HEADERDATA, i); 
        easy[i].SetOpt(CURLoption.CURLOPT_TIMEOUT, timeOut); 
        easy[i].SetOpt(CURLoption.CURLOPT_USERAGENT, UserAgent); 
        Slist sl = new Slist(); 
        sl.Append(Header); 
        easy[i].SetOpt(CURLoption.CURLOPT_HTTPHEADER, sl); 
        easy[i].SetOpt(CURLoption.CURLOPT_POSTFIELDS, post); 
        easy[i].SetOpt(CURLoption.CURLOPT_FOLLOWLOCATION, true); 
        easy[i].SetOpt(CURLoption.CURLOPT_POST, true); 
        //easy[i].SetOpt(CURLoption.CURLOPT_NOBODY, true); 

        if (Url[i].Contains("https")) 
        { 
         easy[i].SetOpt(CURLoption.CURLOPT_SSL_VERIFYHOST, 1); 
         easy[i].SetOpt(CURLoption.CURLOPT_SSL_VERIFYPEER, 0); 
        } 
        multi.AddHandle(easy[i]); 
       } 
      } 

      int stillRunning = 1; 

      while (multi.Perform(ref stillRunning) == CURLMcode.CURLM_CALL_MULTI_PERFORM) ; 

      while (stillRunning != 0) 
      { 
       multi.FDSet(); 
       int rc = multi.Select(1000); // one second 
       switch (rc) 
       { 
        case -1: 
         stillRunning = 0; 
         break; 

        case 0: 
        default: 
         { 
          while (multi.Perform(ref stillRunning) == CURLMcode.CURLM_CALL_MULTI_PERFORM) ; 
          break; 
         } 
       } 
      } 

      // various cleanups 
      multi.Cleanup(); 
      for (int i = 0; i < easy.Length; i++) 
      { 
       easy[i].Cleanup(); 
      } 
      Curl.GlobalCleanup(); 
     } 
     catch (Exception) 
     { 
      //r = ex+""; 
     } 
     return Result; 
    } 

    public static Int32 OnWriteData(Byte[] buf, Int32 size, Int32 nmemb, 
     Object extraData) 
    { 
     int tmp = Convert.ToInt32(extraData.ToString()); ; 
     Result[tmp] += System.Text.Encoding.UTF8.GetString(buf); 
     return size * nmemb; 
    }  
} 

這樣稱呼它:

String[] url= new String[2]; 
url[1]="https://www.google.ca/search?hl=fr&output=search&sclient=psy-ab&q=a1&btnK="; 
url[2]="https://www.google.ca/search?hl=fr&output=search&sclient=psy-ab&q=a2&btnK="; 

string postString=""; // IF YOU DO NOT WANT TO POST ANYTHING YOU CAN DO THE SAME  THING KEEP URL THE SAME SEND POST ARRAY AND CHANGE THE CLASS IT WORKS BOTH WAYS 
String[] result = MultiHttp.MultiPost(url, postString, timeOut); 

它只是一個樣本,但將讓你的工作思路理清你的問題。