首页 > Web开发 > 详细

.NET HTTP异步请求(适用于并发请求同时大于上千上万个)

时间:2020-06-05 00:46:25      阅读:89      评论:0      收藏:0      [点我收藏+]

方法 一:

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();
            }

        }
    }

 

.NET HTTP异步请求(适用于并发请求同时大于上千上万个)

原文:https://www.cnblogs.com/CHPowerljp-IT/p/13046719.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!