C#实现异步GET的方法
2017-10-06 12:35
519 查看
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(); } } }
相关文章推荐
- C#实现异步GET的方法
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)----使用方法
- 转:C#通过WebClient/HttpWebRequest实现http的post/get方法
- 使用jQuery的getJSON方法实现配置信息的状态的异步修改
- C#的path.GetFullPath 获取上级目录实现方法
- [转载]在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)----使用方法
- 深度剖析 C# get post实现方法
- C# 多线程 用委托实现异步_调用委托的BeginInvoke和EndInvoke方法
- C#异步绑定数据实现方法
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- jQuery_review之通过$.get()和$.post()方法来实现异步加载
- C#通过WebClient/HttpWebRequest实现http的post/get方法
- C#的path.GetFullPath 获取上级目录实现方法
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)----使用方法
- jQuery的ajax()检验用户名;通过jQuery的load()/get()/post()方法实现;使用XMLHTTPRequest对象来进行AJAX的异步数据交互
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)----使用方法
- C# 异步实现方法调用
- 在C#中使用异步Socket编程实现TCP网络服务的C/S的通讯构架(二)----使用方法
- C# 异步实现方法调用