WebRequt与WebResponse实现异步请求!
1 using System;
2 using System.Net;
3 using System.Threading;
4 using System.Text;
5 using System.IO;
6 namespace ConsoleApplication1 {
7
8 public class RequestState {
9 const int BufferSize=1024;
10 public StringBuilder RequestData;
11 public byte[] BufferRead;
12 public WebRequest Request;
13 public Stream ResponseStream;
14 public Decoder StreamDecode=Encoding.UTF8.GetDecoder();
15
16 public RequestState() {
17 this.BufferRead=new byte[BufferSize];
18 RequestData=new StringBuilder(String.Empty);
19 Request=null;
20 ResponseStream=null;
21 }
22
23 }
24
25 class ClientGetAsync {
26 public static ManualResetEvent allDone=new ManualResetEvent(false);
27 const int BUFFER_SIZE=1024;
28
29 [STAThread]
30 static void Main(string[] args) {
31 //只能是网址,不能是文件啊????
32 // string strURI="http://nmgsk.onlinedown.net/down/mcombocn.exe";
33 string strURI="http://www.163.com";
34 Uri httpSite=new Uri(strURI);
35 WebRequest wreq=WebRequest.Create(httpSite);
36 RequestState rs=new RequestState();
37 rs.Request=wreq;
38 IAsyncResult r=(IAsyncResult)wreq.BeginGetResponse(
39 new AsyncCallback(RespCallback),rs);
40 allDone.WaitOne();
41 Console.WriteLine(rs.RequestData.ToString());
42 }
43
44 private static void RespCallback(IAsyncResult ar) {
45 RequestState rs=(RequestState)ar.AsyncState;
46 WebRequest req=rs.Request;
47 WebResponse resp=req.EndGetResponse(ar);
48 Stream ResponseStream=resp.GetResponseStream();
49 rs.ResponseStream=ResponseStream;
50 //读指定大小的字节
51 IAsyncResult iarRead=ResponseStream.BeginRead(rs.BufferRead,0,
52 BUFFER_SIZE,new AsyncCallback(ReadCallBack),rs);
53 }
54
55 //异步读取的方式,值得看看,居然是递归调用的形式,头次看到.
56 private static void ReadCallBack(IAsyncResult asyncResult) {
57 RequestState rs=(RequestState)asyncResult.AsyncState;
58 Stream responseStream=rs.ResponseStream;
59 int read=responseStream.EndRead(asyncResult);
60 if (read>0) {
61 Char[] charBuffer=new char[BUFFER_SIZE];
62 int len=rs.StreamDecode.GetChars(rs.BufferRead,0,read,charBuffer,0);
63 string str=new string(charBuffer,0,len);
64 rs.RequestData.Append(Encoding.ASCII.GetString(rs.BufferRead,0,read));
65 IAsyncResult ar=responseStream.BeginRead(
66 rs.BufferRead,0,BUFFER_SIZE,new AsyncCallback(ReadCallBack),rs);
67 }
68 else {
69 if (rs.RequestData.Length>0) {
70 string strContent;
71 strContent=rs.RequestData.ToString();
72 }
73 responseStream.Close();
74 allDone.Set(); //发出信号,使等待的线程继续
75 }
76 return;
77 }
78 }
79 }
80
2 using System.Net;
3 using System.Threading;
4 using System.Text;
5 using System.IO;
6 namespace ConsoleApplication1 {
7
8 public class RequestState {
9 const int BufferSize=1024;
10 public StringBuilder RequestData;
11 public byte[] BufferRead;
12 public WebRequest Request;
13 public Stream ResponseStream;
14 public Decoder StreamDecode=Encoding.UTF8.GetDecoder();
15
16 public RequestState() {
17 this.BufferRead=new byte[BufferSize];
18 RequestData=new StringBuilder(String.Empty);
19 Request=null;
20 ResponseStream=null;
21 }
22
23 }
24
25 class ClientGetAsync {
26 public static ManualResetEvent allDone=new ManualResetEvent(false);
27 const int BUFFER_SIZE=1024;
28
29 [STAThread]
30 static void Main(string[] args) {
31 //只能是网址,不能是文件啊????
32 // string strURI="http://nmgsk.onlinedown.net/down/mcombocn.exe";
33 string strURI="http://www.163.com";
34 Uri httpSite=new Uri(strURI);
35 WebRequest wreq=WebRequest.Create(httpSite);
36 RequestState rs=new RequestState();
37 rs.Request=wreq;
38 IAsyncResult r=(IAsyncResult)wreq.BeginGetResponse(
39 new AsyncCallback(RespCallback),rs);
40 allDone.WaitOne();
41 Console.WriteLine(rs.RequestData.ToString());
42 }
43
44 private static void RespCallback(IAsyncResult ar) {
45 RequestState rs=(RequestState)ar.AsyncState;
46 WebRequest req=rs.Request;
47 WebResponse resp=req.EndGetResponse(ar);
48 Stream ResponseStream=resp.GetResponseStream();
49 rs.ResponseStream=ResponseStream;
50 //读指定大小的字节
51 IAsyncResult iarRead=ResponseStream.BeginRead(rs.BufferRead,0,
52 BUFFER_SIZE,new AsyncCallback(ReadCallBack),rs);
53 }
54
55 //异步读取的方式,值得看看,居然是递归调用的形式,头次看到.
56 private static void ReadCallBack(IAsyncResult asyncResult) {
57 RequestState rs=(RequestState)asyncResult.AsyncState;
58 Stream responseStream=rs.ResponseStream;
59 int read=responseStream.EndRead(asyncResult);
60 if (read>0) {
61 Char[] charBuffer=new char[BUFFER_SIZE];
62 int len=rs.StreamDecode.GetChars(rs.BufferRead,0,read,charBuffer,0);
63 string str=new string(charBuffer,0,len);
64 rs.RequestData.Append(Encoding.ASCII.GetString(rs.BufferRead,0,read));
65 IAsyncResult ar=responseStream.BeginRead(
66 rs.BufferRead,0,BUFFER_SIZE,new AsyncCallback(ReadCallBack),rs);
67 }
68 else {
69 if (rs.RequestData.Length>0) {
70 string strContent;
71 strContent=rs.RequestData.ToString();
72 }
73 responseStream.Close();
74 allDone.Set(); //发出信号,使等待的线程继续
75 }
76 return;
77 }
78 }
79 }
80
一点说明:为什么在标题中要嵌入英文?原因是为了能够让国外的网友能查询到这篇文章。平常在Google上查资料的时候,经常参考国外网友的博客,帮助我解决了很多问题,所以我也想让他们能够参考我写的内容。当然文中我不可能全部译为英文,所以我尽量把代码粘全,靠代码说话吧。

浙公网安备 33010602011771号