直接使用 response.GetResponseStream()返回的流会因为网络延时问题.导致有些代码提示.
stream Is corrupted 流已损坏.
需要先下载到内存中.转换哼内存流以后在给其它代码使用.
/// <summary>
/// 从url读取内容到内存MemoryStream流中
/// </summary>
/// <param name="url"></param>
/// <returns></returns>
private static MemoryStream DownLoadFielToMemoryStream(string url)
{
var wreq = HttpWebRequest.Create(url) as HttpWebRequest;
HttpWebResponse response = wreq.GetResponse() as HttpWebResponse;
MemoryStream ms = null;
using (var stream = response.GetResponseStream())
{
Byte[] buffer = new Byte[response.ContentLength];
int offset = 0, actuallyRead = 0;
do
{
actuallyRead = stream.Read(buffer, offset, buffer.Length - offset);
offset += actuallyRead;
}
while (actuallyRead > 0);
ms = new MemoryStream(buffer);
}
response.Close();
return ms;
}