Click to See Complete Forum and Search --> : [RESOLVED] Downloading a file from a redirected path
yraen
November 4th, 2008, 10:08 AM
I'm trying to create an application to update files locally from a zip file located on a website, however the zip file is only acessed via a redirected weblink and when I try to download the file it errors out.
WebClient _client = new WebClient();
string _toPath = "C:\\Program Files\\EQMapUpdater\\Hold";
if (!Directory.Exists(_toPath))
Directory.CreateDirectory(_toPath);
string _fromPath = "http://www.mapfiend.net/fetchpack/ALL";
_client.DownloadFile(_fromPath, _toPath);
Any ideas on how to do this or if it is even possible?
DeepT
November 4th, 2008, 03:03 PM
Does recursion mean anything to you?
Here is some pseudo code to demonstrate.public void DownloadFile(string FilePath)
{
FileGetter GetFile(FilePath);
If ( GetFile.IsRedirect == true )
{
DownloadFile(GetFile.RedirectPath);
}
else
{
GetFile.Download();
}
}That will handle a theoretical infinite amount of re-directs (until you run out of stack space) and download the file.
You might want to also put in a member that is a counter of how many redirects and limit it to some number otherwise you could get in a circular redirect pattern which will eventually crash your program.
yraen
November 4th, 2008, 03:16 PM
Hadn't thought of doing it with recursion actually, I'll give that a try when I get off work. Thanks! :)
yraen
November 5th, 2008, 04:18 PM
Got it to work by using a different method. Seems HTTPWebRequest has a .AllowAutoRedirect value
using System.IO;
using System.Net;
private Stream _responseStream;
private Stream _localStream;
private HttpWebRequest _webRequest;
private HttpWebResponse _webResponse;
using (WebClient _webClient = new WebClient())
{
try
{
//Create WebRequest to the MapFiend link, allow redirects, and set
default credentials
_webRequest = (HttpWebRequest)WebRequest.Create("http://www.mapfiend.net/fetchpack/ALL");
_webRequest.AllowAutoRedirect = true;
_webRequest.Credentials = CredentialCache.DefaultCredentials;
//Retrieve the response from the server
_webResponse = (HttpWebResponse)_webRequest.GetResponse();
//Get file size from server
Int64 fileSize = _webResponse.ContentLength;
//Open the URL for download
_responseStream = _webClient.OpenRead("http://www.mapfiend.net/fetchpack/ALL");
//Create a new file stream where we will be saving the zip file
_localStream = new FileStream("C:\\Hold\\mapfiend_ALL.zip",
FileMode.Create, FileAccess.Write, FileShare.None);
//Store the number of bytes received from server
int bytesSize = 0;
//A buffer for storing and writing the data retrieved from the server
byte[] downBuffer = new byte[2048];
//Loop through the buffer until the buffer is empty
while ((bytesSize = _responseStream.Read(downBuffer, 0, downBuffer.Length)) > 0)
{
//Write the data from the buffer to the local hard drive
_localStream.Write(downBuffer, 0, bytesSize);
}
}
finally
{
//When the above code has ended, close the streams
_responseStream.Close();
_localStream.Close();
}
}
codeguru.com
Copyright Internet.com Inc., All Rights Reserved.