WebClient 二
WebClient 一种已对WebClient从网页读取数据做了简要的介绍。其中对异步读取网页数据流进一步剖析。
本文还将对WebClient 上传和下载左详细的分析。
1 // 异步调用
2 WebClient webClient = new WebClient();
3 webClient.Credentials = CredentialCache.DefaultCredentials;
4 webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(OpenReadCallback);
5 webClient.OpenReadAsync(new Uri("http://www.cnblogs.cn/"));
6
7 public static void OpenReadCallback(Object sender, OpenReadCompletedEventArgs e)
8 {
9 Stream reply = null;
10 StreamReader s = null;
11
12 try
13 {
14 reply = (Stream)e.Result;
15 s = new StreamReader(reply);
16 string strResult = s.ReadToEnd();
17 AppLog.Write(strResult, AppLog.LogMessageType.Error);
18 }
19 finally
20 {
21 if (s != null)
22 {
23 s.Close();
24 }
25
26 if (reply != null)
27 {
28 reply.Close();
29 }
30 }
31 }
2 WebClient webClient = new WebClient();
3 webClient.Credentials = CredentialCache.DefaultCredentials;
4 webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(OpenReadCallback);
5 webClient.OpenReadAsync(new Uri("http://www.cnblogs.cn/"));
6
7 public static void OpenReadCallback(Object sender, OpenReadCompletedEventArgs e)
8 {
9 Stream reply = null;
10 StreamReader s = null;
11
12 try
13 {
14 reply = (Stream)e.Result;
15 s = new StreamReader(reply);
16 string strResult = s.ReadToEnd();
17 AppLog.Write(strResult, AppLog.LogMessageType.Error);
18 }
19 finally
20 {
21 if (s != null)
22 {
23 s.Close();
24 }
25
26 if (reply != null)
27 {
28 reply.Close();
29 }
30 }
31 }
WebClient 方法 OpenReadAsync 访问Url 参数指定的页码数据流 Steam的事例,该方法是自动从线程池分配线程自语获取,数据流获取有效同时接到同时,出发OpenReadCompleted事件,
故必须为改时间添加去就数据流程方法。本实例中通过调用注册 OpenReadCallback时间来获取读取数据流资源。其中那个对OpenReadAsync 主要实现其功能如下:
1
2
3 public void OpenReadAsync(Uri address)
4 {
5 OpenReadAsync(address, null);
6 }
7
8 public void OpenReadAsync(Uri address, object userToken)
9 {
10 if (address == null)
11 throw new ArgumentNullException("address");
12
13 lock (this)
14 {
15 SetBusy();
16 async = true;
17
18 async_thread = new Thread(delegate(object state)
19 {
20 object[] args = (object[])state;
21 WebRequest request = null;
22 try
23 {
24 request = SetupRequest((Uri)args[0]);
25 WebResponse response = GetWebResponse(request);
26 Stream stream = response.GetResponseStream();
27 OnOpenReadCompleted( new OpenReadCompletedEventArgs(stream, null, false, args[1]));
28 }
29 catch (ThreadInterruptedException)
30 {
31 if (request != null)
32 request.Abort();
33
34 OnOpenReadCompleted(new OpenReadCompletedEventArgs(null, null, true, args[1]));
35 }
36 catch (Exception e)
37 {
38 OnOpenReadCompleted(new OpenReadCompletedEventArgs(null, e, false, args[1]));
39 }
40 });
41 object[] cb_args = new object[] { address, userToken };
42 async_thread.Start(cb_args);
43 }
44 }
45
3 public void OpenReadAsync(Uri address)
4 {
5 OpenReadAsync(address, null);
6 }
7
8 public void OpenReadAsync(Uri address, object userToken)
9 {
10 if (address == null)
11 throw new ArgumentNullException("address");
12
13 lock (this)
14 {
15 SetBusy();
16 async = true;
17
18 async_thread = new Thread(delegate(object state)
19 {
20 object[] args = (object[])state;
21 WebRequest request = null;
22 try
23 {
24 request = SetupRequest((Uri)args[0]);
25 WebResponse response = GetWebResponse(request);
26 Stream stream = response.GetResponseStream();
27 OnOpenReadCompleted( new OpenReadCompletedEventArgs(stream, null, false, args[1]));
28 }
29 catch (ThreadInterruptedException)
30 {
31 if (request != null)
32 request.Abort();
33
34 OnOpenReadCompleted(new OpenReadCompletedEventArgs(null, null, true, args[1]));
35 }
36 catch (Exception e)
37 {
38 OnOpenReadCompleted(new OpenReadCompletedEventArgs(null, e, false, args[1]));
39 }
40 });
41 object[] cb_args = new object[] { address, userToken };
42 async_thread.Start(cb_args);
43 }
44 }
45
SetupRequest((Uri)args[0]) 方法主要功能是 WebRequest.Create(address) 还有代理 验证及相关头文件,GetWebResponse(request) 方法主要功能是 request.GetResponse();OpenReadCompletedEventHandler 是此事件的委托。OpenReadCompletedEventArgs 类为事件处理程序提供事件数据。
对于通过Url 资源读取数据流的WebClient出了OpenRead 和 OpenReadAsync 以外还有那些了。那我告诉你采用相同的原理和内核操作的还有如下方法 。这个将在WebClient三种介绍