Similar to something I posted a few messages up... you'll want to modify this to be a bit more asynchronous, especially if you're downloading large files like videos. This function downloads the contents of the web file and stores it in a temp file and returns the temp file name.
public string GetWebFile(string url)
{
HttpWebRequest webRequest = null;
HttpWebResponse webResponse = null;
Stream responseStream = null;
FileStream fileStream = null;
string filename = "";
try
{
// Create the web request
webRequest = (HttpWebRequest)HttpWebRequest.Create(url);
// Get the response
webResponse = (HttpWebResponse)webRequest.GetResponse();
// Get the response stream
responseStream = webResponse.GetResponseStream();
// write it to a temporary file in 1000 byte chunks
filename = Path.GetTempFileName();
fileStream = new FileStream(filename, FileMode.Append);
int bytesRead = 0;
byte\[\] data = new byte\[1000\];
// keep looping until we read nothing
do
{
// read the next 1000 bytes of data
bytesRead = responseStream.Read(data, 0, 1000);
// write the data to the file
fileStream.Write(data, 0, bytesRead);
}
while (bytesRead > 0);
}
catch (Exception e)
{
MessageBox.Show("Error : " + e.Message);
}
finally
{
// free any resources
if (fileStream != null)
{
fileStream.Close();
fileStream.Dispose();
}
if (responseStream != null)
{
responseStream.Close();
responseStream.Dispose();
}
if (webResponse != null)
{
webResponse.Close();
}
}
// return the contents
return filename;
}