zoukankan      html  css  js  c++  java
  • .NET HTTP异步请求(适用于并发请求同时大于上千上万个)

    方法 一:

    WebRequest Request= WebRequest.Create(strURL);
    Request.BeginGetResponse(new AsyncCallback(OnResponse), Request);

    protected void OnResponse(IAsyncResult ar)
    {
       WebRequest wrq = (WebRequest)ar.AsyncState;
       WebResponse wrs = wrq.EndGetResponse(ar);

       // read the response ...
    }

    方法二:

    class Program
        {
            private const string url = "http://";
            static async Task Main(string[] args)
            {
                await  AsyncTestTask();
            }
    
          
    
            public static async Task AsyncTestTask()
            {
                Console.WriteLine("当前任务Id是:"+Thread.CurrentThread.ManagedThreadId);
                Console.WriteLine(nameof(AsyncTestTask));
                using (var client = new WebClient())
                {
                    string content = await client.DownloadStringTaskAsync(url);
                    Console.WriteLine("当前任务Id是:"+Thread.CurrentThread.ManagedThreadId);
                    Console.WriteLine(content.Substring(0,100));
                    Console.ReadLine();
                }
    
            }
        }
  • 相关阅读:
    数据结构化
    爬取校园新闻首页的新闻
    网络爬虫基础练习
    Hadoop综合大作业
    理解MapReduce
    熟悉常用的HBase操作
    熟悉常用的HDFS操作
    爬虫大作业
    数据结构化与保存
    爬取校园新闻首页的新闻
  • 原文地址:https://www.cnblogs.com/CHPowerljp-IT/p/13046719.html
Copyright © 2011-2022 走看看