using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Net; using System.IO; using System.Threading; namespace AsncyHandle { public class RequestState { const int BUFFER_SIZE = 1024; public StringBuilder RequestData; public byte[] BufferRead; public HttpWebRequest Request; public Stream ResponseStream; //创建适当编码类型的解码器 public Decoder StreamDecode = Encoding.UTF8.GetDecoder(); public RequestState() { BufferRead = new byte[BUFFER_SIZE]; RequestData = new StringBuilder(""); Request = null; ResponseStream = null; } } class Program { public static ManualResetEvent allDone = new ManualResetEvent(false); const int BUFFER_SIZE = 1024; static void Main(string[] args) { Uri HttpSite = new Uri("http://www.baidu.com"); //创建请求对象 HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(HttpSite); //创建请求对象 RequestState rs = new RequestState(); //将请求添加到状态,以便可以被来回传涕 rs.Request = wreq; //发出异步请求 IAsyncResult r = (IAsyncResult)wreq.BeginGetResponse(new AsyncCallback(RespCallback), rs); // 将 ManualResetEvent 设置为 Wait, // 以便在调用回调前,应用程序不退出 allDone.WaitOne(); } public static void showusage() { Console.WriteLine("尝试获取 (GET) 一个 URL"); Console.WriteLine(" 用法::"); Console.WriteLine("ClientGetAsync URL"); Console.WriteLine("示例::"); Console.WriteLine("ClientGetAsync http://www.microsoft.com/net/"); } public static void RespCallback(IAsyncResult ar) { //从异步结果获取RequestState 对象 RequestState rs = (RequestState)ar.AsyncState; //从RequestState 获取HttpWebRequest HttpWebRequest req = rs.Request; //调用 EndGetResponse 生成 HttpWebResponse 对象 HttpWebResponse resp = (HttpWebResponse)req.EndGetResponse(ar); // // 既然我们拥有了响应,就该从 // 响应流开始读取数据了 Stream responseStream = resp.GetResponseStream(); //该读取操作也使用异步完成,所以我们 //将要以 RequestState 存储流 rs.ResponseStream = responseStream; // 请注意,rs.BufferRead 被传入到 BeginRead。 // 这是数据将被读入的位置。 IAsyncResult iarRead = responseStream.BeginRead(rs.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), rs); } private static void ReadCallBack(IAsyncResult asyncResult) { //从asyncresult 获取 RequestState 对象 RequestState rs = (RequestState)asyncResult.AsyncState; //取出在respCallBack 中设置的ResponseStream Stream responseStream = rs.ResponseStream; int read = responseStream.EndRead(asyncResult); if (read > 0) { //准备Char数组缓冲区,用于向Unicode 转换 Char[] charBuffer = new Char[BUFFER_SIZE]; //将字节流转换为Char 数组,然后转换为字符串 //len 显示多少字符被转换为 unicode int len = rs.StreamDecode.GetChars(rs.BufferRead, 0, read, charBuffer, 0); String str = new String(charBuffer, 0, len); //将最近读取的数据追加到RequestData stirngBuilder对象中 //该对想包含在RequestState中 rs.RequestData.Append(str); IAsyncResult ar = responseStream.BeginRead(rs.BufferRead, 0, BUFFER_SIZE, new AsyncCallback(ReadCallBack), rs); } else { if(rs.RequestData.Length>1) { //所有数据都已被读取,因此将其显示 到控制台 string strContent; strContent = rs.RequestData.ToString(); Console.WriteLine(strContent); } //关闭响应流 responseStream.Close(); //设置ManualResetEvent ,以便主线程可以退出 allDone.Set(); } return; } } }