/// <summary> ///http Download File/// </summary> /// <param name= "url" >Download file Address</param> /// <param name= "path" >file storage address, including file name</param> /// <returns></returns> Public BOOLHttpdownload (stringUrlstringpath) { stringTempPath = System.IO.Path.GetDirectoryName (Path) +@"\temp"; System.IO.Directory.CreateDirectory (TempPath); //Create a temporary file directory stringTempfile = TempPath +@"\"+ System.IO.Path.GetFileName (Path) +". Temp";//Temporary Files if(System.IO.File.Exists (tempfile)) {System.IO.File.Delete (tempfile); //exists then delete } Try{FileStream fs=NewFileStream (Tempfile, Filemode.append, FileAccess.Write, fileshare.readwrite); //Setting ParametersHttpWebRequest request = webrequest.create (URL) asHttpWebRequest; //send request and get corresponding response dataHttpWebResponse response = Request. GetResponse () asHttpWebResponse; //until request. The GetResponse () program only starts sending a POST request to the destination Web pageStream Responsestream =Response. GetResponseStream (); //creating a local file write stream//Stream stream = new FileStream (tempfile, filemode.create); byte[] BArr =New byte[1024x768]; intSize = Responsestream.read (BARR,0, (int) barr.length); while(Size >0) { //Stream. Write (BARR, 0, size);Fs. Write (BARR,0, size); Size= Responsestream.read (BARR,0, (int) barr.length); } //Stream. Close ();FS. Close (); Responsestream.close (); System.IO.File.Move (tempfile, path); return true; } Catch(Exception ex) {return false; } }
C # http Download file