This Post Contains Affiliate Links
Downloading a file via HTTP post and HTTP get in C#
Previously, I had written two posts on how to upload files to a web server, one for the case when the size of the HTTP request is small, and the other for the case when the size of the HTTP request is large. How about the downloading of files from a web server? In this post, I shall discuss how to download files from a HTTP server via the System.Net.HttpWebRequest class.
Download a file via HTTP get
Downloading of a file from the web server via HTTP get in C# consists of three main steps:
- Construct the HTTP get request to send to the web server.
- Send the HTTP request and get the HTTP response from the web server.
- Save the contents in the HTTP response to a local file.
Construct the HTTP get request
The following code segment prepares an instance of the System.Net.HttpWebRequest class to download my website logo.
// Construct HTTP request to get the logo HttpWebRequest httpRequest = (HttpWebRequest) WebRequest.Create("http://www.techcoil.com/ph/img/logo.png"); httpRequest.Method = WebRequestMethods.Http.Get;
Send the HTTP request and get the HTTP response from the web server.
// Get back the HTTP response for web server HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse(); Stream httpResponseStream = httpResponse.GetResponseStream();
Save the file to your local disk
// Define buffer and buffer size int bufferSize = 1024; byte[] buffer = new byte[bufferSize]; int bytesRead = 0; // Read from response and write to file FileStream fileStream = File.Create("techcoil-logo.png"); while ((bytesRead = httpResponseStream.Read(buffer, 0, bufferSize)) != 0) { fileStream.Write(buffer, 0, bytesRead); } // end while
Download a file via HTTP post
Sometimes, there is a need for the client to supply some information to the web server in order to download a file. This can be a case when we want to control how files are downloaded. We can refer to every file in our web server with a unique id and write a server script to serve the respective file based on the id received from the client. For the sake of demonstration, I had published a server script, "/poc/downloadPng.php", that will read a post variable labelled as id. If id is 1, it will write my website logo to the client, else it will write my 404 icon image to the client.
As with HTTP get, downloading of a file from the web server via HTTP post in C# consists of three main steps:
- Construct the HTTP post request to send to the web server.
- Send the HTTP request and get the HTTP response from the web server.
- Save the contents in the HTTP response to a local file.
Since step 2 and 3 are identical, I will just discuss step 1.
Construct the HTTP post request to send to the web server.
// Construct HTTP request to get the file HttpWebRequest httpRequest = (HttpWebRequest) WebRequest.Create("http://www.techcoil.com/poc/downloadPng.php"); httpRequest.Method = WebRequestMethods.Http.Post; // Include post data in the HTTP request string postData = "id=1"; httpRequest.ContentLength = postData.Length; httpRequest.ContentType = "application/x-www-form-urlencoded"; // Write the post data to the HTTP request StreamWriter requestWriter = new StreamWriter( httpRequest.GetRequestStream(), System.Text.Encoding.ASCII); requestWriter.Write(postData); requestWriter.Close();
Namespaces to include
The following namespaces need to be included in order for the above mentioned codes to compile.
- System.IO
- System.Net
- System.Text
Related posts
- Handling web server communication feedback with System.Net.WebException in C#
- Sending a file and some form data via HTTP post in C#
- Uploading large HTTP multipart request with System.Net.HttpWebRequest in C#
- How to build a web based user interaction layer in C#
- How to send HTTP post requests and HTTP get requests using jQuery
- PHP codes to tell browsers to open the download dialog box for users to download a file
This Post Contains Affiliate Links
Source: https://www.techcoil.com/blog/downloading-a-file-from-via-http-post-and-http-get-in-c/
Posted by: carsonarring1990.blogspot.com
0 Response to "This Post Contains Affiliate Links"
Post a Comment