This is for all of you C# .NET web developers out there. Been having todo a lot of ridiculous clean up lately which normally includes lots of offline batch processes. I found this method to be uber useful, I'm sure most of you will too.
public void StoreUrlToFile(string url, string destination, int timeout)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.Timeout = timeout;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if (HttpStatusCode.OK == response.StatusCode)
{
using (Stream stream = response.GetResponseStream())
{
using (FileStream fileStream = new FileStream(destination, FileMode.OpenOrCreate, FileAccess.Write))
{
byte[] buffer = new byte[4096];
int bytesRead;
while (0 < (bytesRead = stream.Read(buffer, 0, buffer.Length)))
{
fileStream.Write(buffer, 0, bytesRead);
}
}
}
}
}
feed