C#实现异步GET的方法
作者:优雅先生 时间:2022-08-17 11:52:55
本文实例讲述了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();
}
}
}
希望本文所述对大家的C#程序设计有所帮助。
标签:C#,异步
0
投稿
猜你喜欢
使用Maven配置Spring的方法步骤
2023-02-05 18:37:46
Java Swing实现让窗体居中显示的方法示例
2023-11-06 02:59:07
Java创建型设计模式之建造者模式详解
2023-10-09 17:25:13
android实现倒计时功能代码
2022-06-28 03:57:31
Java反射机制深入理解
2022-11-24 14:58:14
基于C#实现的三层架构实例
2023-09-09 11:39:33
深入浅析jni中的java接口使用
2023-07-22 19:54:23
java使用JNA(Java Native Access)调用dll的方法
2022-02-06 09:27:34
java简单实现数组中的逆序对
2022-06-26 02:23:17
java8 stream中Collectors.toMap空指针问题及解决
2023-01-16 13:05:28
详解二维码生成工厂
2022-09-26 11:26:13
JAVA使用JDBC连接oracle数据库的详细过程
2021-11-20 19:20:19
android图像绘制(六)获取本地图片或拍照图片等图片资源
2021-07-26 14:20:51
Java设计模式之享元模式
2022-01-30 19:03:28
Android使用 Coroutine + Retrofit打造简单的HTTP请求库
2023-11-17 04:56:11
java8的stream如何取max
2023-09-06 19:08:37
C语言实现支持动态拓展和销毁的线程池
2023-05-11 10:47:42
Android自定义View接收输入法输入的内容
2022-08-28 12:25:42
Android 打开相册选择单张图片实现代码
2023-05-29 17:06:38
Java8生成时间方式及格式化时间的方法实例
2021-07-28 19:58:52