download - Downloading files from remote server -


I am using C # and Console app and I use this script to download files from a remote server I am doing Some things in that area that I want to add first, when he writes a file, it does not take into account a new line. It's running a certain amount of bytes and then goes on a new line I would like to keep it in the same format as it is reading the file. Second, there are several .jpg files on the server that I need to download. How can I use this script to download jpg files

  Public Static Ent Download Files (String Remote URL, string localfile) {int bytesProcessed = 0; // Assign these objects to these objects so that they can block the stream at the end of the stream streamer RemoteStream = null; Streamer local stream = null; WebResponse response = null; // Use an attempt / hold / ultimately block, leave exceptions on both WebRequest and Stream // classes error attempts. {// Create request for specified remote file name WebRequest request = WebRequest.Create (remoteUrl); request. Authentic = true; Network credential credentials = new network credentials ("id", "pass"); request. Credential = Credentials; If (Request! = Null) {// Send request to the server and // WebRPSPage object response = Receive the request. GetResponse (); If (response! = Null) {// Once the WebResponse object has been retrieved, // Section object related to the data of the response Remote Stream = New Streamer (Reaction GaterpoonsStream ()); // local file create local stream = new streamer (file. (Local file); // Allocation of a 1k buffer letter [] buffer = new four [1024]; Int bytes read; // Simplify / read data from the loop stream / until you return any bytes, read data from // bytes (up to 1k) from stream bytes = RemoteStream Read (buffer, 0, buffer length); // Write the data in the local file localStream.WriteLine (buffer, 0, bytes read); // Increase total bytes processed byte processed + = bytes read; } While (bytes read> 0); }}} Hold (exception e) {Console.WriteLine (e.Message); } Finally close the {// response and make the objects currents here / ensure that they are closed, even if an exception / thrown at some point (reaction! = Zero) response.Close (); If (remote stream! = Tap) remote stream. Stop it (); If Local Stream (= Null) Local Stream Stop it (); } // Return total processed total bytes to call. Return processed byte;    

Why do not you use WebClient.DownloadData Or WebClient.DownloadFile instead?

  Webclient client = new webclient (); Client.Credentials = New network credentials ("id", "pass"); Client.DownloadFile (remote url, local file);  

Well the correct way to copy one stream to another is not what you did. You should not read at char [] , as you can run at the end of encoding and line issues because you are downloading a binary file. WriteLine method call is also problematic The right way to copy the contents of a stream into another is:

  Zero copystream (stream destination, stream source) {int count ; Byte [] buffer = new byte [BUFFER_SIZE]; To use  (count = source.Read (buffer, 0, buffer, langeline)) type gt; destination (buffer, 0, count);}  

> WebClient class is very easy and instead I suggest using it.


Comments