0

我正在尝试创建一个程序,它可以对与任何指定的谷歌搜索相关的结果数量进行排序。我需要一张非常快的大桌子,所以我考虑使用循环。每次我尝试它时,调试器都会由于“System.Windows.Markup.XamlParseException”而崩溃。

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 循环中的 main 中使用,例如:

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

我知道这是问题所在,因为只要我对循环发表评论,或者将其降低到一个代表,就没有任何问题。我试过了:

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

它没有用

4

1 回答 1

0

我不认为你正在做的事情是正确的方法。我可以告诉你的最简单的方法是使用 Lib curl c#。您可以发送一个 url 数组并以数组的形式获取响应。这将非常适合您在这里所需要的。下面是一个执行多任务处理的示例类代码。您只需发送网址。

 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);

它只是一个示例,但会为您提供解决问题的工作思路。

于 2013-08-24T19:28:37.777 回答