0

我正在使用以下代码从网站并行获取响应,但它仅并行执行 3 个 url。我必须一次运行超过 1000 个 url。如何实现这一点。请就此提出建议。提前致谢。

class Program
{
    static void Main(string[] args)
    {
        DateTime dtStartDate = DateTime.Now;
        Parallel.For(0, 4, new ParallelOptions { MaxDegreeOfParallelism = 4 },
      i =>
      {
          var task = MakeAsyncRequest("http://..../Test.aspx", "text/html");
          Console.WriteLine("Got response of {0}", task.Result);
      });
        Console.WriteLine(DateTime.Now.Subtract(dtStartDate).ToString());
        Console.ReadLine();
    }

    public static Task<string> MakeAsyncRequest(string url, string contentType)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
        request.ContentType = contentType;
        request.Method = WebRequestMethods.Http.Get;
        request.Timeout = 20000;
        request.Proxy = null;

        Task<WebResponse> task = Task.Factory.FromAsync(
            request.BeginGetResponse,
            asyncResult => request.EndGetResponse(asyncResult),
            (object)null);

        return task.ContinueWith(t => ReadStreamFromResponse(t.Result));
    }

    private static string ReadStreamFromResponse(WebResponse response)
    {
        using (Stream responseStream = response.GetResponseStream())
        using (StreamReader sr = new StreamReader(responseStream))
        {
            string strContent = sr.ReadToEnd();
            if (strContent != "")
                return "OK";
        }
        return "empty";
    }
}
4

0 回答 0