Thursday 27 November 2014

C# code to download a file from a website URL

Hi All,
Yesterday one of my colleagues was having issue with downloading a file from URL using c# for one of the projects. This is a general requirement for many projects and thought it would be helpful if I post here.

using System;
using System.IO;
using System.Net;
using System.Text;

namespace FileDownloadSample
{
    class Program
    {
        static void Main(string[] args)
        {

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.yoursite.com/yourfile.pdf");

            request.Method = "GET";

            var encoding = new UTF8Encoding();

            request.Headers.Add(HttpRequestHeader.AcceptLanguage, "en-gb,en;q=0.5");
            request.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");

            request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
            request.UserAgent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:12.0) Gecko/20100101 Firefox/12.0";

            var resp = (HttpWebResponse)request.GetResponse();

           

            using (var stream = File.Create("TargetFileName.pdf"))
                resp.GetResponseStream().CopyTo(stream);


        }
    }
}

Thursday 14 August 2014

How to Replace characters at specified position and line in UNIX


sed -n -e 24p -e 24q | cut -c27-34
sed -n '24p;24q' | cut -c27-34
The -n option means 'do not print lines by default'; the 24p means print line 24; the 24q means quit after processing line 24. You could leave that out, in which case sed would continue processing the input, effectively ignoring it.