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;
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);
}
}
}
0 comments:
Post a Comment