12篇學通C#網絡編程——第三篇 HTTP應用編程(下)


 

    第三篇來的好晚啊,上一篇說了如何向服務器推送信息,這一篇我們看看如何"快好准"的從服務器下拉信息。

    網絡上有很多大資源文件,比如供人下載的zip包,電影(你懂的),那么我們如何快速的進行下載,大家第一反應肯定就是多線程下載,

那么這些東西是如何做的呢?首先我們可以從“QQ的中轉站里面拉一個rar下來“。

然后用fiddler監視一下,我們會發現一個有趣的現象:

第一:7.62*1024*1024≈7990914  千真萬確是此文件

第二:我明明是一個http鏈接,tmd的怎么變成n多個了?有意思。

好,我們繼續往下看,看看這些鏈接都做了些什么?

最終,我們發現http協議中有一個Conent—Range字段,能夠把我們的文件總大小進行切分,然后並行下載,最后再進行合並,大概我們知道

了什么原理,那么,我們強大的C#類庫提供了AddRange來獲取Http中資源的指定范圍。

 

既然進行了切分,那么首先一定要知道文件的ContentLength是多少,如果對http協議比較熟悉的話,當發送一個頭信息過去,服務器返回的

頭信息中會包含很多東西,此時我們就知道要下載資源的大概情況,這個就有點“兵馬未動,糧草先行“的感覺。

 1             var request = (HttpWebRequest)HttpWebRequest.Create(url);
 2 
 3             request.Method = "Head";
 4 
 5             request.Timeout = 3000;
 6 
 7             var response = (HttpWebResponse)request.GetResponse();
 8 
 9             var code = response.StatusCode;
10 
11             if (code != HttpStatusCode.OK)
12             {
13                 Console.WriteLine("下載資源無效!");
14                 return;
15             }
16 
17             var total = response.ContentLength;

 

這里有個決策,到底是以下載量來決定線程數,還是以線程數來決定下載量,由於我們的下載取決於當前的網速,所以在這種場合下更好的方案是

采用后者,這幾天在閃存里面兩次看到蒼老師,肅然起敬,所以決定在不用線程和線程的情況下,看看下載倉老師的速度如何。

圖片大小(217.27KB)

View Code
  1 using System;
  2 using System.Collections.Generic;
  3 using System.Linq;
  4 using System.Text;
  5 using System.Net;
  6 using System.Threading;
  7 using System.Threading.Tasks;
  8 using System.IO;
  9 using System.Collections.Concurrent;
 10 using System.Diagnostics;
 11 using System.Drawing;
 12 
 13 
 14 namespace ConsoleApplication1
 15 {
 16     public class Program
 17     {
 18         public static CountdownEvent cde = new CountdownEvent(0);
 19 
 20         //每個線程下載的字節數,方便最后合並
 21         public static ConcurrentDictionary<long, byte[]> dic = new ConcurrentDictionary<long, byte[]>();
 22 
 23         //請求文件
 24         public static string url = "http://www.pncity.net/bbs/data/attachment/forum/201107/30/1901108yyd8gnrs2isadrr.jpg";
 25 
 26         static void Main(string[] args)
 27         {
 28             for (int i = 0; i < 1; i++)
 29             {
 30                 Console.WriteLine("\n****************************\n第{0}次比較\n****************************", (i + 1));
 31 
 32                 //不用線程
 33                 //RunSingle();
 34 
 35                 //使用多線程
 36                 RunMultiTask();
 37             }
 38 
 39             Console.Read();
 40         }
 41 
 42         static void RunMultiTask()
 43         {
 44             Stopwatch watch = Stopwatch.StartNew();
 45 
 46             //開5個線程
 47             int threadCount = 5;
 48 
 49             long start = 0;
 50 
 51             long end = 0;
 52 
 53             var total = GetSourceHead();
 54 
 55             if (total == 0)
 56                 return;
 57 
 58             var pageSize = (int)Math.Ceiling((Double)total / threadCount);
 59 
 60             cde.Reset(threadCount);
 61 
 62             Task[] tasks = new Task[threadCount];
 63 
 64             for (int i = 0; i < threadCount; i++)
 65             {
 66                 start = i * pageSize;
 67 
 68                 end = (i + 1) * pageSize - 1;
 69 
 70                 if (end > total)
 71                     end = total;
 72 
 73                 var obj = start + "|" + end;
 74 
 75                 tasks[i] = Task.Factory.StartNew(j => new DownFile().DownTaskMulti(obj), obj);
 76             }
 77 
 78             Task.WaitAll(tasks);
 79 
 80             var targetFile = "C://" + url.Substring(url.LastIndexOf('/') + 1);
 81 
 82             FileStream fs = new FileStream(targetFile, FileMode.Create);
 83 
 84             var result = dic.Keys.OrderBy(i => i).ToList();
 85 
 86             foreach (var item in result)
 87             {
 88                 fs.Write(dic[item], 0, dic[item].Length);
 89             }
 90 
 91             fs.Close();
 92 
 93             watch.Stop();
 94 
 95             Console.WriteLine("多線程:下載耗費時間:{0}", watch.Elapsed);
 96         }
 97 
 98         static void RunSingle()
 99         {
100             Stopwatch watch = Stopwatch.StartNew();
101 
102             if (GetSourceHead() == 0)
103                 return;
104 
105             var request = (HttpWebRequest)HttpWebRequest.Create(url);
106 
107             var response = (HttpWebResponse)request.GetResponse();
108 
109             var stream = response.GetResponseStream();
110 
111             var outStream = new MemoryStream();
112 
113             var bytes = new byte[10240];
114 
115             int count = 0;
116 
117             while ((count = stream.Read(bytes, 0, bytes.Length)) != 0)
118             {
119                 outStream.Write(bytes, 0, count);
120             }
121 
122             var targetFile = "C://" + url.Substring(url.LastIndexOf('/') + 1);
123 
124             FileStream fs = new FileStream(targetFile, FileMode.Create);
125 
126             fs.Write(outStream.ToArray(), 0, (int)outStream.Length);
127 
128             outStream.Close();
129 
130             response.Close();
131 
132             fs.Close();
133 
134             watch.Stop();
135 
136             Console.WriteLine("不用線程:下載耗費時間:{0}", watch.Elapsed);
137         }
138 
139         //獲取頭信息
140         public static long GetSourceHead()
141         {
142             var request = (HttpWebRequest)HttpWebRequest.Create(url);
143 
144             request.Method = "Head";
145             request.Timeout = 3000;
146 
147             var response = (HttpWebResponse)request.GetResponse();
148 
149             var code = response.StatusCode;
150 
151             if (code != HttpStatusCode.OK)
152             {
153                 Console.WriteLine("下載的資源無效!");
154                 return 0;
155             }
156 
157             var total = response.ContentLength;
158 
159             Console.WriteLine("當前資源大小為:" + total);
160 
161             response.Close();
162 
163             return total;
164         }
165     }
166 
167     public class DownFile
168     {
169         // 多線程下載
170         public void DownTaskMulti(object obj)
171         {
172             var single = obj.ToString().Split('|');
173 
174             long start = Convert.ToInt64(single.FirstOrDefault());
175 
176             long end = Convert.ToInt64(single.LastOrDefault());
177 
178             var request = (HttpWebRequest)HttpWebRequest.Create(Program.url);
179 
180             request.AddRange(start, end);
181 
182             var response = (HttpWebResponse)request.GetResponse();
183 
184             var stream = response.GetResponseStream();
185 
186             var outStream = new MemoryStream();
187 
188             var bytes = new byte[10240];
189 
190             int count = 0;
191 
192             while ((count = stream.Read(bytes, 0, bytes.Length)) != 0)
193             {
194                 outStream.Write(bytes, 0, count);
195             }
196 
197             outStream.Close();
198 
199             response.Close();
200 
201             Program.dic.TryAdd(start, outStream.ToArray());
202 
203             Program.cde.Signal();
204         }
205     }
206 }

 

 

      在下面的圖中可以看出,我們的資源被分成了n段,在217.27KB的情況下,多線程加速還不是很明顯,我們可以試試更大的文件,這里我就

在本地放一個133M的rar文件。

        //請求文件
        public static string url = "http://localhost:56933/1.rar";

現在看一下效果是非常明顯的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM