C#HTTPWebRequest多线程

1 c# multithreading thread-safety

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

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);
        }
    }
}
}
Run Code Online (Sandbox Code Playgroud)

如果有人会纠正上面的代码,真的很感激.

谢谢

flq*_*flq 5

我会说

  • 废除你的代表
  • 在循环中设置WebRequest
  • 使用获取响应的异步版本 (Begin/End)GetResponse
  • 保持异步回调可重入(独立于任何实例状态)并使其使用"结束"调用的结果和您传入的任何状态(例如WebRequest本身)

这应该或多或少有效