多线程HTTPWebRequest

多线程HTTPWebRequest

本文介绍了C#多线程HTTPWebRequest的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我是新手.我正在尝试使用多线程发送HTTP Web请求,但我无法实现我所需要的.
我的要求是将请求发送到数千个相同或不同的网站,并解析我从httpwebrequest获得的响应.
在下面的代码中,我正在发送2个同时线程,我正在寻找10个同时线程.

Hi all,

I am new to threading. I am trying to send HTTP Web Request using multi threading, I am not able to acheive what I need.
My requirement is to send request to thousands of same or different websites and parse the response i get it from httpwebrequest.
In the below code, i am sending 2 simulteaneous threads, I am looking for ten simultaneously threads.

namespace threading
{
    public partial class Form1 : Form
    {
        delegate string UrlFetcher(string url);

        private void button1_Click(object sender, EventArgs e)
        {
            int i = 1;
            UrlFetcher u = new UrlFetcher(Fetch);
            UrlFetcher u = new UrlFetcher(Fetch1);
            string pageURL = "http://www.google.com";

            while (i <= 1000)
            {
                u.BeginInvoke(pageURL, new AsyncCallback(AfterFetch), "this is state");
                i++;
                u.BeginInvoke(pageURL, new AsyncCallback(AfterFetch1), "this is state");
                i++;
                Thread.Sleep(5);
            }
        }

        static string Fetch(string pageURL)
        {
            HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(pageURL);
            WebReq.Method = "GET";
            HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
            Stream Answer = WebResp.GetResponseStream();
            StreamReader _Answer = new StreamReader(Answer);
            string myString = _Answer.ReadToEnd();
            return myString;
        }

        void AfterFetch(IAsyncResult result)
        {
            string a;

            AsyncResult async = (AsyncResult)result;
            UrlFetcher fetcher = (UrlFetcher)async.AsyncDelegate;
            a = fetcher.EndInvoke(result).ToString();

            Regex regx = new Regex(@"<td>([A-Za-z0-9\-]+)\.(com|net)</td>", RegexOptions.IgnoreCase);
            MatchCollection mactches = regx.Matches(a);
            foreach (Match match in mactches)
            {
                string pattern = @"<(.|\n)*?>";
                string r = Regex.Replace(match.Value, pattern, string.Empty);
                textBox3.AppendText(r);
            }
        }

        static string Fetch1(string pageURL)
        {
            HttpWebRequest WebReq = (HttpWebRequest)WebRequest.Create(pageURL);
            WebReq.Method = "GET";
            HttpWebResponse WebResp = (HttpWebResponse)WebReq.GetResponse();
            Stream Answer = WebResp.GetResponseStream();
            StreamReader _Answer = new StreamReader(Answer);
            string myString = _Answer.ReadToEnd();
            return myString;
        }

        void AfterFetch1(IAsyncResult result)
        {
            string a;

            AsyncResult async = (AsyncResult)result;
            UrlFetcher fetcher = (UrlFetcher)async.AsyncDelegate;
            a = fetcher.EndInvoke(result).ToString();

            Regex regx = new Regex(@"<td>([A-Za-z0-9\-]+)\.(com|net)</td>", RegexOptions.IgnoreCase);
            MatchCollection mactches = regx.Matches(a);
            foreach (Match match in mactches)
            {
                string pattern = @"<(.|\n)*?>";
                string r = Regex.Replace(match.Value, pattern, string.Empty);
                textBox3.AppendText(r);
            }
        }
    }
}




如果有人可以更正上面的代码,我们将不胜感激.

谢谢




If anyone would correct the above code, it is really appreciated.

Thanks

推荐答案


这篇关于C#多线程HTTPWebRequest的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-15 10:52