{"id":132,"date":"2011-10-15T22:23:10","date_gmt":"2011-10-15T14:23:10","guid":{"rendered":"https:\/\/www.techcoil.com\/blog\/?p=132"},"modified":"2018-11-14T10:41:53","modified_gmt":"2018-11-14T02:41:53","slug":"downloading-a-file-from-via-http-post-and-http-get-in-c","status":"publish","type":"post","link":"https:\/\/www.techcoil.com\/blog\/downloading-a-file-from-via-http-post-and-http-get-in-c\/","title":{"rendered":"Downloading a file via HTTP post and HTTP get in C#"},"content":{"rendered":"<p>Previously, I had written two posts on how to upload files to a web server, <a href=\"\/blog\/sending-a-file-and-some-form-data-via-http-post-in-c\/\" target=\"_blank\" title=\"Post on sending a file and some form data via HTTP post in C#\">one<\/a> for the case when the size of the <a href=\"https:\/\/www.techcoil.com\/glossary\/http-request\/\" rel=\"noopener\" target=\"_blank\">HTTP request<\/a> is small, and <a href=\"\/blog\/uploading-large-http-multipart-request-with-system-net-httpwebrequest-in-c\/\" target=\"_blank\" title=\"Post on uploading large HTTP multipart request with System.Net.HttpWebRequest in C#\">the other<\/a> 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 <a href=\"https:\/\/www.techcoil.com\/glossary\/http-server\/\" rel=\"noopener\" target=\"_blank\">HTTP server<\/a> via the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.httpwebrequest.aspx\" title=\"MSDN reference on System.Net.HttpWebRequest class\">System.Net.HttpWebRequest<\/a> class. <\/p>\n<h3>Download a file via HTTP get<\/h3>\n<p>Downloading of a file from the web server via HTTP get in C# consists of three main steps:<\/p>\n<ol>\n<li>Construct the HTTP get request to send to the web server.<\/li>\n<li>Send the HTTP request and get the <a href=\"https:\/\/www.techcoil.com\/glossary\/http-response\/\" rel=\"noopener\" target=\"_blank\">HTTP response<\/a> from the web server.<\/li>\n<li>Save the contents in the HTTP response to a local file.<\/li>\n<\/ol>\n<h4>Construct the HTTP get request<\/h4>\n<p>The following code segment prepares an instance of the System.Net.HttpWebRequest class to download my <a href=\"http:\/\/www.techcoil.com\/ph\/img\/logo.png\" target=\"_blank\" title=\"Techcoil logo\">website logo<\/a>.<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ Construct HTTP request to get the logo\r\nHttpWebRequest httpRequest = (HttpWebRequest)\r\n    WebRequest.Create(&quot;http:\/\/www.techcoil.com\/ph\/img\/logo.png&quot;);\r\nhttpRequest.Method = WebRequestMethods.Http.Get;\r\n<\/pre>\n<h4>Send the HTTP request and get the HTTP response from the web server.<\/h4>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ Get back the HTTP response for web server\r\nHttpWebResponse httpResponse \r\n    = (HttpWebResponse)httpRequest.GetResponse();\r\nStream httpResponseStream = httpResponse.GetResponseStream();\r\n<\/pre>\n<h4>Save the file to your local disk<\/h4>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ Define buffer and buffer size\r\nint bufferSize = 1024;\r\nbyte&#x5B;] buffer = new byte&#x5B;bufferSize];\r\nint bytesRead = 0;\r\n\r\n\/\/ Read from response and write to file\r\nFileStream fileStream = File.Create(&quot;techcoil-logo.png&quot;);\r\nwhile ((bytesRead = httpResponseStream.Read(buffer, 0, bufferSize)) != 0) \r\n{\r\n    fileStream.Write(buffer, 0, bytesRead);\r\n} \/\/ end while\r\n<\/pre>\n<h3>Download a file via HTTP post<\/h3>\n<p>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 <a href=\"\/ph\/img\/logo.png\" target=\"_blank\" title=\"Website logo\">website logo<\/a> to the client, else it will write my <a href=\"\/ph\/img\/404-icon.png\" target=\"_blank\" title=\"404 icon image\">404 icon image<\/a> to the client.     <\/p>\n<p>As with HTTP get, downloading of a file from the web server via HTTP post in C# consists of three main steps:<\/p>\n<ol>\n<li>Construct the HTTP post request to send to the web server.<\/li>\n<li>Send the HTTP request and get the HTTP response from the web server.<\/li>\n<li>Save the contents in the HTTP response to a local file.<\/li>\n<\/ol>\n<p>Since step 2 and 3 are identical, I will just discuss step 1. <\/p>\n<h4>Construct the HTTP post request to send to the web server.<\/h4>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\r\n\/\/ Construct HTTP request to get the file\r\nHttpWebRequest httpRequest = (HttpWebRequest)\r\n    WebRequest.Create(&quot;http:\/\/www.techcoil.com\/poc\/downloadPng.php&quot;);\r\nhttpRequest.Method = WebRequestMethods.Http.Post;\r\n\r\n\/\/ Include post data in the HTTP request\r\nstring postData = &quot;id=1&quot;;\r\nhttpRequest.ContentLength = postData.Length;\r\nhttpRequest.ContentType = &quot;application\/x-www-form-urlencoded&quot;;\r\n\/\/ Write the post data to the HTTP request\r\nStreamWriter requestWriter = new StreamWriter(\r\n    httpRequest.GetRequestStream(), \r\n    System.Text.Encoding.ASCII);\r\nrequestWriter.Write(postData);\r\nrequestWriter.Close();\r\n<\/pre>\n<h3>Namespaces to include<\/h3>\n<p>The following namespaces need to be included in order for the above mentioned codes to compile.<\/p>\n<ul>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.io.aspx\" target=\"_blank\" title=\"MSDN reference for the System.IO namespace\">System.IO<\/a><\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.aspx\" target=\"_blank\" title=\"MSDN reference for the System.Net namespace\">System.Net<\/a><\/li>\n<li><a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.text.aspx\" target=\"_blank\" title=\"MSDN reference for the System.Text namespace\">System.Text<\/a><\/li>\n<\/ul>\n<h3>Related posts<\/h3>\n<ul>\n<li><a href=\"\/blog\/handling-web-server-communication-feedback-with-system-net-webexception\/\" target=\"_blank\">Handling web server communication feedback with System.Net.WebException in C#<\/a><\/li>\n<li><a href=\"\/blog\/sending-a-file-and-some-form-data-via-http-post-in-c\/\" target=\"_blank\">Sending a file and some form data via HTTP post in C#<\/a><\/li>\n<li><a href=\"\/blog\/uploading-large-http-multipart-request-with-system-net-httpwebrequest-in-c\/\" target=\"_blank\">Uploading large HTTP multipart request with System.Net.HttpWebRequest in C#<\/a><\/li>\n<li><a href=\"\/blog\/how-to-build-a-web-based-user-interaction-layer-in-c\/\" target=\"_blank\">How to build a web based user interaction layer in C#<\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/how-to-send-http-post-requests-and-http-get-requests-using-jquery\/\" title=\"How to send HTTP post requests and HTTP get requests using jQuery\" target=\"_blank\">How to send HTTP post requests and HTTP get requests using jQuery<\/a><\/li>\n<li><a href=\"http:\/\/www.techcoil.com\/blog\/php-codes-to-tell-browsers-to-open-the-download-dialog-box-for-users-to-download-a-file\/\" title=\"PHP codes to tell browsers to open the download dialog box for users to download a file\" target=\"_blank\">PHP codes to tell browsers to open the download dialog box for users to download a file<\/a><\/li>\n<\/ul>\n\n      <ul id=\"social-sharing-buttons-list\">\n        <li class=\"facebook\">\n          <a href=\"https:\/\/www.facebook.com\/sharer\/sharer.php?u=https%3A%2F%2Fwp.me%2Fp245TQ-28\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n            <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Facebook.png\" alt=\"Facebook icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"twitter\">\n          <a href=\"https:\/\/twitter.com\/intent\/tweet?text=&url=https%3A%2F%2Fwp.me%2Fp245TQ-28&via=Techcoil_com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Twitter.png\" alt=\"Twitter icon\"> Tweet\n          <\/a>\n        <\/li>\n        <li class=\"linkedin\">\n          <a href=\"https:\/\/www.linkedin.com\/shareArticle?mini=1&title=&url=https%3A%2F%2Fwp.me%2Fp245TQ-28&source=https:\/\/www.techcoil.com\" target=\"_blank\" role=\"button\" rel=\"nofollow\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/linkedin.png\" alt=\"Linkedin icon\"> Share\n          <\/a>\n        <\/li>\n        <li class=\"pinterest\">\n          <a href=\"https:\/\/pinterest.com\/pin\/create\/button\/?url=https%3A%2F%2Fwww.techcoil.com%2Fblog%2Fwp-json%2Fwp%2Fv2%2Fposts%2F132&description=\" class=\"pin-it-button\" target=\"_blank\" role=\"button\" rel=\"nofollow\" count-layout=\"horizontal\">\n          <img decoding=\"async\" src=\"\/ph\/img\/3rd-party\/social-icons\/Pinterest.png\" alt=\"Pinterest icon\"> Save\n          <\/a>\n        <\/li>\n      <\/ul>\n    ","protected":false},"excerpt":{"rendered":"<p>Previously, I had written two posts on how to upload files to a web server, <a href=\"\/blog\/sending-a-file-and-some-form-data-via-http-post-in-c\/\" target=\"_blank\" title=\"Post on sending a file and some form data via HTTP post in C#\">one<\/a> for the case when the size of the HTTP request is small, and <a href=\"\/blog\/uploading-large-http-multipart-request-with-system-net-httpwebrequest-in-c\/\" target=\"_blank\" title=\"Post on uploading large HTTP multipart request with System.Net.HttpWebRequest in C#\">the other<\/a> 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 <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/system.net.httpwebrequest.aspx\" title=\"MSDN reference on System.Net.HttpWebRequest class\">System.Net.HttpWebRequest<\/a> class. <\/p>\n","protected":false},"author":1,"featured_media":1189,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":true,"_jetpack_newsletter_tier_id":0,"footnotes":""},"categories":[375],"tags":[20,50,23,51],"jetpack_featured_media_url":"https:\/\/www.techcoil.com\/blog\/wp-content\/uploads\/C-Sharp-Logo.gif","jetpack_shortlink":"https:\/\/wp.me\/p245TQ-28","jetpack-related-posts":[],"jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/132"}],"collection":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/comments?post=132"}],"version-history":[{"count":0,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/posts\/132\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media\/1189"}],"wp:attachment":[{"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/media?parent=132"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/categories?post=132"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.techcoil.com\/blog\/wp-json\/wp\/v2\/tags?post=132"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}