/// <summary>
/// 根据文件虚拟路径获取内存流
/// </summary>
/// <param name="url">http路径</param>
/// <returns></returns>
public static Stream GetUrlStream(string url)
{
try
{
HttpWebRequest myRequest = (HttpWebRequest)HttpWebRequest.Create(url);// 打开网络连接
myRequest.AddRange(0);
Stream readStream = myRequest.GetResponse().GetResponseStream();// 向服务器请求,获得服务器的回应数据流
MemoryStream memoryStream = new MemoryStream();
//将基础流写入内存流
const int bufferLength = 1024;
byte[] buffer = new byte[bufferLength];
memoryStream.Position = 0;
int contentSize = readStream.Read(buffer, 0, bufferLength);
while (contentSize > 0)
{
memoryStream.Write(buffer, 0, contentSize);
contentSize = readStream.Read(buffer, 0, bufferLength);
memoryStream.Position = memoryStream.Length;//重新定义流的追加位置
}
readStream.Close();
return memoryStream;
}
catch (Exception ex)
{
throw ex;
}
}