C#多线程HTTPWebRequest [英] C# Multithreading HTTPWebRequest

查看:357
本文介绍了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

推荐答案

那甚至不应该编译.您在按钮单击均匀处理程序中两次声明了相同的变量.我认为您是要声明uu1.

其次,您不会生成线程.

第三,十个线程可能太多.考虑使用线程池,一次执行两个或三个.
这是有关使用线程池的文章:

多线程,委托和自定义事件 [
That shouldn''t even compile. You''re declaring the same variable twice in the button click even handler. I think you meant to declare u and u1.

Second, you''re not spawning threads.

Third, 10 threads may be too much. Consider using a thread pool and do two or three at a time.

Here''s an article about using thread pools:

Multithreading, Delegates, and Custom Events[^]


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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆