首页  编辑  

下载指定文件

Tags: /C#/Web网络编成/   Date Created:

using System . IO;

using System . Net;

HttpWebRequest vHttpWebRequest = ( HttpWebRequest ) WebRequest . Create(

    @"http://album.sina.com.cn/pic_3/589d32f502000001" );

HttpWebResponse vHttpWebResponse = ( HttpWebResponse )vHttpWebRequest . GetResponse();

BinaryReader vBinaryReader = new BinaryReader (vHttpWebResponse . GetResponseStream());

FileStream vFileStream = new FileStream ( @"c:\temp\temp.gif" , FileMode . Create);

BinaryWriter vBinaryWriter = new BinaryWriter (vFileStream);

byte [] vBuffer = new byte [ 0x1000 ];

int vReadLength = vBinaryReader . Read(vBuffer, 0 , vBuffer . Length);

while (vReadLength > 0 )

{

   vBinaryWriter . Write(vBuffer, 0 , vReadLength);

   vReadLength = vBinaryReader . Read(vBuffer, 0 , vBuffer . Length);

}

vHttpWebResponse . Close();

vBinaryWriter . Close();

vFileStream . Close();

// or

WebClient vWebClient = new WebClient ();

vWebClient . DownloadFile( @"http://album.sina.com.cn/pic_3/589d32f502000001" ,

    @"c:\temp\temp.gif" );