最近做项目的时候,要求对音频文件做地址保护,找了一些资料,关于如果对地址做防盗链的处理还是比较多的,只是都是写好的组件,有些是开源的。但是根据项目要求,结合公司基础产品的情况,还是需要单独的来处理,就没有用那些组件。这就涉及到了对网络文件的读取。
很自然的会想到HttpWebRequest和HttpWebResponse,不错,我也正是这么处理的。看看第一个版本:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
第一个版本--问题版本
1![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
private static byte[] getFileStream(string fileUrl)
{
2
HttpWebRequest request = WebRequest.Create(fileUrl) as HttpWebRequest;
3
HttpWebResponse response = null;
4
MemoryStream msstream=new MemoryStream();
5
byte []buffer=new byte[1024];
6
if (request != null)
7![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8
try
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
10
response = request.GetResponse() as HttpWebResponse;
11
if (response != null)
12![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
13
if (response.StatusCode == HttpStatusCode.OK)
14![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
15
Stream ms = response.GetResponseStream();
16
17
while (ms.Read(buffer, 0, buffer.Length) > 0)
18
msstream.Write(buffer, 0, buffer.Length);
19
}
20
}
21![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
}
23
catch (Exception ex)
24![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
25
ex.Message.ToString();
26
}
27
finally
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
29
response.Close();
30
}
31
}
32![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
return msstream.ToArray();
34
}
35![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
问题就出在发出请求后,得到的文件大小与实际的文件大小不一样,变大了。跟踪调试后,问题:
1)跟踪调试,得到的结果是做了两次请求,但是并没有弹出两个下载文件对话框出来,而是只有一个。
2)ms的内容长度与最后得到的msstream的长度是不一致的。
对问题1很容易解决,问题2是怎么造成的?这才是真正的关键所在。当最后ms的内容长度小于所要读取的长度的时候,问题就出现了,buffer里面的内容有‘杂质’了,写进msstream里面的内容自然就不对了。直接用ms.Length做判断不就可以了,为什么要用上面的方式进行读取?那就先看看ms.Length在msdn里面怎么给出解释的:
![](https://images.cnblogs.com/cnblogs_com/huankfy/length.jpg)
我并没有从Stream里面继承一个新类,而是直接用的Stream,length属性是用不了的(其它需要在继承类里面实现的熟悉也是一样)。换个思路:
![](https://www.cnblogs.com/Images/OutliningIndicators/ContractedBlock.gif)
Code---正确版本
1![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
static byte[] getInternetFileContent(string fileUlr)
{
2
HttpWebRequest request = null;
3
HttpWebResponse response = null;
4
MemoryStream msstream = new MemoryStream();
5![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
6![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
try
{
7
request = WebRequest.Create(fileUlr) as HttpWebRequest;
8
response = request.GetResponse() as HttpWebResponse;
9![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (response != null)
{
10![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
if (response.StatusCode == HttpStatusCode.OK)
{
11
Stream ms = response.GetResponseStream();
12
long len = response.ContentLength;
13
byte[] buffer = new byte[1024];
14
long counter = 1024;
15
long mod = len % counter;
16![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
while (len / counter > 0)
{
17
len -= counter;
18
msstream.Write(buffer, 0, buffer.Length);
19
}
20
ms.Read(buffer, 0, (int)mod);
21
msstream.Write(buffer, 0, (int)mod);
22
}
23
}
24
}
25![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
catch (Exception ex)
{
26
System.Diagnostics.Trace.Write(ex.Message);
27
}
28![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
finally
{
29
request = null;
30
response = null;
31
}
32
return msstream.ToArray();
33
}
增加了个计数器进行判断,最后一次读取的流就不会有‘杂质’了,得到的内容就是完整的文件内容,不会多出其它的东西。一切正常。