WebClient:用DownloadFile和用OpenRead的结果怎么不同?顺请荐书:)
多谢!代码如下:
System.Net.WebClient client = new System.Net.WebClient();
client.Headers.Add ("user-agent", "WinampMPEG/2.7");
client.DownloadFile("http://localhost/m3uplay/tomp3.asp?SongID=148040","d:\\c.mp3");
这样得到的结果是一个5.01Mb的文件。
如果这样:
System.IO.Stream str = client.OpenRead("http://localhost/m3uplay/tomp3.asp?SongID=148040");
System.IO.FileStream fs = new FileStream("d:\\this.mp3",System.IO.FileMode.Create,System.IO.FileAccess.Write,System.IO.FileShare.Read);
byte[] buffer = new byte[1024];
int count;
int ps=0;
for(;;)
{
count=str.Read(buffer,0,1024);
ps+=count;
fs.Write(buffer,0,1024);
down=((float)ps)/(2048*1024);
fs.Flush();
if(count==0)break;
}
fs.Close();
str.Close();
这样得到的就是一个6M的文件,而且mp3播放起来很刺耳。
这是怎么回事?我要怎样能得到正确的结果呢?
btw:我最近总是觉得我在网络方面基础很差,可否推荐一些书来读?