博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
C#实现异步GET的方法
阅读量:5081 次
发布时间:2019-06-12

本文共 2487 字,大约阅读时间需要 8 分钟。

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

 

转载于:https://www.cnblogs.com/dj1232090/p/7631378.html

你可能感兴趣的文章
Scrapy实战篇(三)之爬取豆瓣电影短评
查看>>
HDU 5510 Bazinga KMP
查看>>
[13年迁移]Firefox下margin-top问题
查看>>
Zookeeper常用命令 (转)
查看>>
Java程序IP v6与IP v4的设置
查看>>
RUP(Rational Unified Process),统一软件开发过程
查看>>
数据库链路创建方法
查看>>
Enterprise Library - Data Access Application Block 6.0.1304
查看>>
重构代码 —— 函数即变量(Replace temp with Query)
查看>>
Bootstrap栅格学习
查看>>
程序员的数学
查看>>
聚合与组合
查看>>
jQuery如何获得select选中的值?input单选radio选中的值
查看>>
设计模式 之 享元模式
查看>>
如何理解汉诺塔
查看>>
洛谷 P2089 烤鸡【DFS递归/10重枚举】
查看>>
15 FFT及其框图实现
查看>>
Linux基本操作
查看>>
osg ifc ifccolumn
查看>>
C++ STL partial_sort
查看>>