title: Download File from Given URL
body: <p>Can someone tell me a good way of getting the file extension when trying to download file from the given URI?
  At present I am using WebClient to download file.
  I am getting the mime type and using that I am mapping it to extensions.</p>
  
  <p>Here this is a custom webclient which depending on <code>HeadOnly</code> property either return the data or just header. </p>
  
  <pre><code>public class SlideWebClient : WebClient {
      public bool HeadOnly { get; set; }
          protected override WebRequest GetWebRequest(Uri address) {
              WebRequest req = base.GetWebRequest(address);
              if (HeadOnly &amp;&amp; req.Method == "GET") {
                  req.Method = "HEAD";
              }
              return req;
          }
      }
  }
  
  
  public class FileDownloader {           
  
      /// &lt;summary&gt;
      /// Function to download a file from URL and save it to local drive
      /// &lt;/summary&gt;
      /// &lt;param name="_URL"&gt;URL address to download file&lt;/param&gt;
      public static void DownloadFile(Uri source, string destination) {
          try {
              using (WebClient _WebClient = new WebClient()) {
                  // Downloads the resource with the specified URI 
                  // to a local file.
                  _WebClient.DownloadFile(source, destination);
              }
          } catch (Exception _Exception) {
              // Error
              Console.WriteLine("Exception caught in process: {0}", 
                  _Exception.ToString());
          }
      }
  
      /// &lt;summary&gt;
      ///Get the Content type of file to be downloaded  for given URI
      /// &lt;/summary&gt;
      /// &lt;returns&gt;&lt;/returns&gt;
      public static String GetContentType(Uri url) {
          using (SlideWebClient client = new SlideWebClient()) {
              client.HeadOnly = true;
              // note should be 0-length
              byte[] body = client.DownloadData(url); 
              return client.ResponseHeaders["content-type"];
          }
      }
  
      public static bool  IsPdf(string contentType) {
          if (contentType.Contains("application/pdf")) return true;
          else return false;
      }
  }
  </code></pre>