C#實現非同步GET的方法

來源:互聯網
上載者:User

標籤:nta   text   null   public   event   非同步get   send   callback   open   

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的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.