C#实现异步GET的方法

 更新时间:2015年07月11日 10:36:01   作者:优雅先生  
这篇文章主要介绍了C#实现异步GET的方法,涉及C#异步请求的相关实现技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了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#Winform窗口移动方法

    C#Winform窗口移动方法

    今天小编就为大家分享一篇C#Winform窗口移动方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2018-12-12
  • 字符串阵列String[]转换为整型阵列Int[]的实例

    字符串阵列String[]转换为整型阵列Int[]的实例

    下面小编就为大家分享一篇字符串阵列String[]转换为整型阵列Int[]的实例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2017-12-12
  • C#中WebBroeser控件用法实例教程

    C#中WebBroeser控件用法实例教程

    这篇文章主要介绍了C#中WebBroeser控件用法,包括了常用属性、事件处理及应用实例,需要的朋友可以参考下
    2014-09-09
  • C#开发微信门户及应用(1) 微信接口使用

    C#开发微信门户及应用(1) 微信接口使用

    这篇文章主要为大家详细介绍了C#开发微信门户及应用第一篇,微信接口的使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-06-06
  • C#取得Web程序和非Web程序的根目录的N种取法总结

    C#取得Web程序和非Web程序的根目录的N种取法总结

    C#取得Web程序和非Web程序的根目录的N种取法,方便大家知道,有更好的方法,请说明
    2008-03-03
  • c#获取光标在屏幕中位置的简单实例

    c#获取光标在屏幕中位置的简单实例

    这篇文章主要介绍了c#获取光标在屏幕中位置的简单实例,有需要的朋友可以参考一下
    2013-12-12
  • C#算法之散列表

    C#算法之散列表

    本文详细讲解了C#算法之散列表,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • C#中重写tospring方法的实现

    C#中重写tospring方法的实现

    重写ToString方法允许你自定义对象的字符串表示形式,本文主要介绍了C#中重写tospring方法的实现,具有一定的参考价值,感兴趣的可以了解一下
    2024-08-08
  • C#仿QQ实现简单的截图功能

    C#仿QQ实现简单的截图功能

    这篇文章主要为大家详细介绍了如何利用C#语言模拟QQ实现屏幕选择区域截图功能,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下
    2022-08-08
  • C#基础入门之算法:交换

    C#基础入门之算法:交换

    本文主要介绍了C#中算法:交换的相关知识,具有很好的参考价值。下面跟着小编一起来看下吧
    2017-03-03

最新评论