7,659
社区成员




private bool DownloadMp3(string url, long start, long end, string path)
{
HttpWebRequest httpRequestIm;
HttpWebResponse httpResponseIm;
Stream respStreamIm;
httpRequestIm = (HttpWebRequest)WebRequest.Create(url);
httpRequestIm.Method = "GET";
httpRequestIm.AddRange((int)start, (int)end);
try
{
httpResponseIm = (HttpWebResponse)httpRequestIm.GetResponse();
if (httpResponseIm.StatusCode == HttpStatusCode.OK || httpResponseIm.StatusCode == HttpStatusCode.PartialContent)
{
respStreamIm = httpResponseIm.GetResponseStream();
long length = httpResponseIm.ContentLength;
byte[] b = new byte[Convert.ToInt32(length)];
int nReadSize = 0;
nReadSize = respStreamIm.Read(b, 0, Convert.ToInt32(length));
while (nReadSize > 0)
{
if(Stop)
return false;
//写文件
}
}
}
catch{}
finally
{
if (respStreamIm != null)
{
respStreamIm.Close();//中途停止下载的话,这句时间很长很长
respStreamIm = null;
}
if (httpResponseIm != null)
{
httpResponseIm.Close();
httpResponseIm = null;
}
if (httpRequestIm != null)
{
httpRequestIm.Abort();
httpRequestIm = null;
}
if (ms != null)
{
ms.Close();
ms = null;
}
}
}