Not logged in - Login

Request a web page/url and save response to file

The following illustrates an example of requesting a web URL and saving the response to a file on the local file system.

string urlToDownload = "https://someplace.com/somefolder/someotherfolder/somepage";
string localDownloadPath = @"c:\temp";


// declare & create the URI object that holds the URL of the web page we want to post to.
System.Uri uriDest = new Uri(urlToDownload);

// declare & create an Http Web Request object using the URI object from above.
System.Net.HttpWebRequest httpReq = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uriDest);

// set the method to GET
httpReq.Method = "GET";

// set the User Agent.
// If this is being done from within a web application, the value
// of the incoming request object could be passed into here, if desired.
httpReq.UserAgent = "My Client";

// build our output file name
string sZipFile = System.IO.Path.Combine(localDownloadPath, "temp.zip");


// Get the response.
using (WebResponse response = httpReq.GetResponse())
using (var strm = response.GetResponseStream()) // get the response stream
using (System.IO.FileStream fs = new FileStream(sZipFile, FileMode.Create)) // get a stream writer
{
   // save the stream to our file
   strm.CopyTo(fs);
}