欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

C#實(shí)現(xiàn)異步GET的方法

 更新時(shí)間:2015年07月11日 10:36:01   作者:優(yōu)雅先生  
這篇文章主要介紹了C#實(shí)現(xiàn)異步GET的方法,涉及C#異步請(qǐng)求的相關(guān)實(shí)現(xiàn)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下

本文實(shí)例講述了C#實(shí)現(xiàn)異步GET的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:

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發(fā)生錯(cuò)誤!" : e.Result);
    }
    #endregion
    #region WebClient的OpenReadAsync測(cè)試
    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("執(zhí)行WebClient的OpenReadAsync出錯(cuò):" + 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();
    }
  }
}

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • C#生成唯一值的方法匯總

    C#生成唯一值的方法匯總

    這篇文章主要介紹了C#生成唯一值的方法匯總,有需要的朋友可以參考一下
    2013-11-11
  • C# JSON格式化轉(zhuǎn)換輔助類(lèi) ConvertJson

    C# JSON格式化轉(zhuǎn)換輔助類(lèi) ConvertJson

    本文介紹使用C#原生代碼實(shí)現(xiàn) JSON格式化以及各種類(lèi)型轉(zhuǎn)化JSON的輔助類(lèi),幫助開(kāi)發(fā)人員快速開(kāi)發(fā)。
    2016-04-04
  • C#學(xué)習(xí)教程之Socket的簡(jiǎn)單使用

    C#學(xué)習(xí)教程之Socket的簡(jiǎn)單使用

    這篇文章主要給大家介紹了關(guān)于C#學(xué)習(xí)教程之Socket的簡(jiǎn)單使用,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-02-02
  • C#減少垃圾回收壓力的字符串操作詳解

    C#減少垃圾回收壓力的字符串操作詳解

    這篇文章給大家詳細(xì)分析了C#減少垃圾回收壓力的字符串操作的相關(guān)知識(shí)點(diǎn),有興趣的朋友參考學(xué)習(xí)下吧。
    2018-03-03
  • C#實(shí)現(xiàn)windows form限制文本框輸入的方法

    C#實(shí)現(xiàn)windows form限制文本框輸入的方法

    這篇文章主要介紹了C#實(shí)現(xiàn)windows form限制文本框輸入的方法,涉及C#限制文本框輸入的技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • C#運(yùn)算符之與,或,異或及移位運(yùn)算小結(jié)

    C#運(yùn)算符之與,或,異或及移位運(yùn)算小結(jié)

    本文是對(duì)C#中的與,或,異或及移位運(yùn)算進(jìn)行了詳細(xì)的介紹,需要的朋友可以過(guò)來(lái)參考下,希望對(duì)大家有所幫助
    2013-10-10
  • C#操作數(shù)據(jù)庫(kù)總結(jié)(vs2005+sql2005)

    C#操作數(shù)據(jù)庫(kù)總結(jié)(vs2005+sql2005)

    C#操作數(shù)據(jù)庫(kù)總結(jié),每次做項(xiàng)目都會(huì)用到數(shù)據(jù)庫(kù),對(duì)數(shù)據(jù)庫(kù)的操作都是糊里糊涂從書(shū)里找代碼用。通過(guò)昨天晚上與今天早上的努力,把數(shù)據(jù)庫(kù)的操作整理了一下,下面把整理結(jié)果做個(gè)小結(jié)
    2012-09-09
  • 基于TCP異步Socket模型的介紹

    基于TCP異步Socket模型的介紹

    本篇文章小編將為大家介紹,基于TCP異步Socket模型的介紹,需要的朋友參考下
    2013-04-04
  • C#引用類(lèi)型轉(zhuǎn)換的常見(jiàn)方式總結(jié)

    C#引用類(lèi)型轉(zhuǎn)換的常見(jiàn)方式總結(jié)

    這篇文章主要介紹了C#引用類(lèi)型轉(zhuǎn)換的常見(jiàn)方式,包括子類(lèi)轉(zhuǎn)換成父類(lèi),父類(lèi)轉(zhuǎn)換成子類(lèi),以及不是子父級(jí)關(guān)系類(lèi)之間的轉(zhuǎn)換,需要的朋友可以參考下
    2014-09-09
  • C# SQLite數(shù)據(jù)庫(kù)入門(mén)使用說(shuō)明

    C# SQLite數(shù)據(jù)庫(kù)入門(mén)使用說(shuō)明

    這篇文章主要給大家介紹了關(guān)于C#中SQLite數(shù)據(jù)庫(kù)入門(mén)使用的相關(guān)資料,文中通過(guò)圖文以及示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2018-11-11

最新評(píng)論