WebClient 三

WebClient(1,2)中只要介绍了关于通过方法OpenRead 和OpenReadAsync 获取数据流,读取Url资源数据流外,WebClient还包括其他方法读取资源

如:DownloadData 和DowloadDataAsync 读取数据流,单核心本质上和openRead没有任何区别,起始还是通过webRequest 和webResponse 获取数据流,

DownloadDate 只是把数量流转换成byte [] 数据,DownloadString 和DownloadStringAsync 起始就是对DownLoadData 直接调用方法。

具体实现和代码做如下:

            


   WebClient webclient 
= new WebClient();
            
byte[] byteData = webclient.DownloadData("http://www.cnblogs.com/");
            
string result = System.Text.Encoding.UTF8.GetString(byteData);
            AppLog.Write(result, AppLog.LogMessageType.Info);

            
//异步调用
            webclient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(DownDataCallback);
            webclient.DownloadDataAsync(
new Uri("http://www.cnblogs.com/"));
 
public static void DownDataCallback(Object sender, DownloadDataCompletedEventArgs e)
        {
            
byte[] reply;
            reply 
= (byte[])e.Result;
            
string strResult = System.Text.Encoding.UTF8.GetString(reply);
            AppLog.Write(strResult, AppLog.LogMessageType.Info);
        }

Webclient DowloadData 中具体是通过通过webRequest 和webResponse 读取指定url数据流,其中本质上和OpenRead() 基本相同,

主要的区别是DownloadData读取数据流抓换成二进制数据byte[],具体实现原代码是怎样呢,以下是DownloadData 的核心代码如下:

 

  1             WebRequest request = WebRequest.Create("address") ;

 2             WebResponse response = request.GetResponse();
 3             Stream stream = response.GetResponseStream();
 4             int length = (int)response.ContentLength;
 5             MemoryStream ms = null;
 6             bool nolength = (length == -1);
 7             int size = ((nolength) ? 8192 : length);
 8             if (nolength)
 9                 ms = new MemoryStream();
10 ;
11             int nread = 0;
12             int offset = 0;
13             byte[] buffer = new byte[size];
14             while ((nread = stream.Read(buffer, offset, size)) != 0)
15             {
16                 if (nolength)
17                 {
18                     ms.Write(buffer, 0, nread);
19                 }
20                 else
21                 {
22                     offset += nread;
23                     size -= nread;
24                 }
25                 if (async)  //异步读取数据
26                 {
27                     OnDownloadProgressChanged(new DownloadProgressChangedEventArgs(nread, length, userToken));
28                 }
29             }

 

和DownloadData  类型DownloadString 功能类型,后者组要是调用前者实现其功能的,看如下DownloaString 代码如下:

 

     1             WebClient webclient = new WebClient();

 2             string result = webclient.DownloadString("http://www.cnblogs.com/");
 3             AppLog.Write(result, AppLog.LogMessageType.Error);
 4 
 5 
 6             //异步调用
 7             webclient.DownloadDataCompleted += new DownloadDataCompletedEventHandler(DownDataCallback);
 8             webclient.DownloadDataAsync(new Uri("http://www.cnblogs.com/"));
 9 
10 
11 
12         public static void DownloadStringCallBack(object sender, DownloadStringCompletedEventArgs e)
13         {
14             string result = e.Result.ToString();
15             AppLog.Write(result, AppLog.LogMessageType.Error);

16         }

 除了实现方法相似外,DownloadString 代码是通过调用方法是通过   encoding.GetString (DownloadData (CreateUri (address))) ,把括DownloadStringAsyn 异步方法也是调用DownloadData 实现其功能。另外还有方法DownloadFile 和 DownloadFileAsync 是制定url 资源下载数据,也是读取数据流,然后保存在本地文件而已,具体功能更

和势力代码如下:由于功能类型,在这里就不再多做解释,把实例代码贴出来供大家参考。 

 

  1  

 2 string fileName = string.Format("{0}\\{1}.txt", Server.MapPath("Log"), DateTime.Now.ToString("ddHHssss"));
 3             webclient.DownloadFile("http://www.cnblogs.com/", fileName);
 4 
 5             //异步调用
 6             webclient.DownloadFileCompleted += new System.ComponentModel.AsyncCompletedEventHandler(DownloadFileCompletedCallback);
 7             webclient.DownloadProgressChanged += new DownloadProgressChangedEventHandler(DownloadProgressCallback);
 8             webclient.DownloadFileAsync(new Uri("http://www.cnblogs.com/"), fileName)
 9 
10         static void DownloadProgressCallback(object sender, DownloadProgressChangedEventArgs e)
11         {
12  
13             string progress = string.Format("{0}    downloaded {1} of {2} bytes. {3} % complete...",
14                 (string)e.UserState,e.BytesReceived,e.TotalBytesToReceive, e.ProgressPercentage);
15             AppLog.Write(progress, AppLog.LogMessageType.Error);
16         }

 

 以上是读取去资源数据信息到本地,如果想上传到指定url指定资源信息。也是类似。未完待续 。。。。。

 

posted @ 2011-04-08 12:25  AndyYu  阅读(3442)  评论(3编辑  收藏  举报