欢迎来到代码驿站!

.NET代码

当前位置:首页 > 软件编程 > .NET代码

C#实现异步GET的方法

时间:2021-02-24 09:37:45|栏目:.NET代码|点击:

本文实例讲述了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#程序设计有所帮助。

上一篇:基于私钥加密公钥解密的RSA算法C#实现方法

栏    目:.NET代码

下一篇:.NET下模拟数组越界的方法详解

本文标题:C#实现异步GET的方法

本文地址:http://www.codeinn.net/misctech/68978.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有