• C#实现异步GET的方法


    using System;
    using System.Collections.Generic;
    using System.Configuration;
    using System.IO;
    using System.Linq;
    using System.Net;
    using System.Text;
    using System.Threading.Tasks;
    namespace WebClientAsynProject
    {
      public class Program
      {
        #region HttpWebRequest异步GET
        public static void AsyncGetWithWebRequest(string url)
        {
          var request = (HttpWebRequest) WebRequest.Create(new Uri(url));
          request.BeginGetResponse(new AsyncCallback(ReadCallback), request);
        }
        private static void ReadCallback(IAsyncResult asynchronousResult)
        {
          var request = (HttpWebRequest) asynchronousResult.AsyncState;
          var response = (HttpWebResponse) request.EndGetResponse(asynchronousResult);
          using (var streamReader = new StreamReader(response.GetResponseStream()))
          {
            var resultString = streamReader.ReadToEnd();
            Console.WriteLine(resultString);
          }
        }
        #endregion
        #region WebClient异步GET
        public static void AsyncGetWithWebClient(string url)
        {
          var webClient = new WebClient();
          webClient.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
          webClient.DownloadStringAsync(new Uri(url));
        }
        private static void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
          //Console.WriteLine(e.Cancelled);
          Console.WriteLine(e.Error != null ? "WebClient异步GET发生错误!" : e.Result);
        }
        #endregion
        #region WebClient的OpenReadAsync测试
        public static void TestGetWebResponseAsync(string url)
        {
          var webClient = new WebClient();
          webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
          webClient.OpenReadAsync(new Uri(url));
        }
        private static void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
          if(e.Error == null)
          {
            var streamReader = new StreamReader(e.Result);
            var result = streamReader.ReadToEnd();
            Console.WriteLine(result);
          }
          else
          {
            Console.WriteLine("执行WebClient的OpenReadAsync出错:" + e.Error);
          }
        }
        #endregion
        public static void Main(string[] args)
        {
          AsyncGetWithWebRequest("http://baidu.com");
          Console.WriteLine("hello");
          AsyncGetWithWebClient("http://baidu.com");
          Console.WriteLine("world");
          TestGetWebResponseAsync("http://baidu.com");
          Console.WriteLine("jxqlovejava");
          Console.Read();
        }
      }
    }
  • 相关阅读:
    springboot开发之配置Servlet三大组件(Servlet、Filter、Listener)
    springboot开发之配置嵌入式Servlet容器两种方式
    springboot开发之利用idea自带的插件模拟客户端请求
    springboot开发之配置自定义的错误界面和错误信息
    springboot开发之删除员工
    springboot开发之修改员工
    springboot开发之添加员工
    springboot开发之thymeleaf页面公共元素的抽取
    oracle 创建表、删除表、添加字段、删除字段、表备注、字段备注、修改表属性
    C#拼装JSON数组简易方法
  • 原文地址:https://www.cnblogs.com/dj1232090/p/7631378.html
Copyright © 2020-2023  润新知