1.11M Members

Download File using http in c#

 
0
 

Hi Guys,
I am developing an windows application which is downloading file from webserver using FTP, my question is can i download file using HTTP. can anybody help me with some source. I am downloading a xml file.


Thanks in advance.

 
0
 

You could do something like this:

using System.IO;
using System.Net;

namespace DW_413504_CS_CON
{
   class Program
   {
      public static void DoWebClientExample(string strURI)
      {
         WebClient wc = new WebClient();
         wc.DownloadFile(strURI, "c:\\install\\txpeng542.exe");
      }

      static void Main(string[] args)
      {
         string strURI = "http://www.textpad.com/download/v54/txpeng542.exe";

         DoWebClientExample(strURI);
      }
   }
}
 
0
 

I made this example using an EXE file which demonstrates the continuity of the download, but it will work either way, but if your file is XML, you could just use the web client method.
Here are examples of both WebClient and HTTPWebRequest (downloads will go into the current user's temp directory):

using System;
using System.IO;
using System.Linq;
using System.Net;

namespace DW_413504_CS_CON
{
   class Program
   {
      public static string _strTempDir = Path.GetTempPath();

      public static void DoWebClientExample(string strURI)
      {
         WebClient wc = new WebClient();
         string strFileName = strURI.Split('/').Last();
         wc.DownloadFile(strURI, Path.Combine(_strTempDir, strFileName));
      }

      public static bool DownloadFile(HttpWebResponse resp, string strFileName, ref string strError)
      {
         bool blnRetVal = true;

         try
         {
            long lngFileSize = resp.ContentLength;

            using (StreamReader fileIn = new StreamReader(resp.GetResponseStream()))
            {
               using (StreamWriter fileOut = new StreamWriter(strFileName))
               {
                  long lngTotalBytesRead = 0;
                  int intBytesRead = 0;

                  byte[] buff = new byte[8192];//size based on observation*2

                  for (int intLoop = 0; lngTotalBytesRead < lngFileSize; intLoop++)
                  {
                     intBytesRead = fileIn.BaseStream.Read(buff, 0, buff.Length);
                     fileOut.BaseStream.Write(buff, 0, intBytesRead);
                     lngTotalBytesRead += intBytesRead;
                  }
                  //
                  fileOut.Flush();
                  fileOut.Close();
               }
               //
               fileIn.Close();
            }
            //
            resp.Close();
         }
         catch (Exception exc)
         {
            blnRetVal = false;
            strError = exc.Message;
         }

         return blnRetVal;
      }

      public static bool DoWebRequestExample(string strURI, ref string strError)
      {
         bool blnRetVal = true;
         try
         {
            HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(strURI);
            req.Method = WebRequestMethods.Http.Get;
            using (HttpWebResponse resp = (HttpWebResponse)req.GetResponse())
            {
               string strFileName = strURI.Split('/').Last();
               if (!DownloadFile(resp, Path.Combine(_strTempDir, strFileName), ref strError))
               {
                  blnRetVal = false;
               }
               //
               resp.Close();
            }
         }
         catch (Exception exc)
         {
            blnRetVal = false;
            strError = exc.Message;
         }

         return blnRetVal;
      }

      static void Main(string[] args)
      {
         string strError = "";
         string strURI = "http://www.textpad.com/download/v54/txpeng542.exe";
         
         if (!DoWebRequestExample(strURI, ref strError))
         {
            Console.WriteLine("Could not download file: " + strError);
            return;
         }

         DoWebClientExample(strURI);
      }
   }
}
 
0
 

Thanks thines01.

Question Answered as of 2 Years Ago by thines01
You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article