dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: [RESOLVED] Downloading a file from a redirected path

  1. #1
    Join Date
    Apr 2008
    Location
    Kentucky
    Posts
    73

    [RESOLVED] Downloading a file from a redirected path

    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.

    Code:
    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?

  2. #2
    Join Date
    Sep 2004
    Posts
    1,361

    Re: Downloading a file from a redirected path

    Does recursion mean anything to you?

    Here is some pseudo code to demonstrate.
    Code:
    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.

  3. #3
    Join Date
    Apr 2008
    Location
    Kentucky
    Posts
    73

    Re: Downloading a file from a redirected path

    Hadn't thought of doing it with recursion actually, I'll give that a try when I get off work. Thanks!

  4. #4
    Join Date
    Apr 2008
    Location
    Kentucky
    Posts
    73

    Re: Downloading a file from a redirected path

    Got it to work by using a different method. Seems HTTPWebRequest has a .AllowAutoRedirect value

    Code:
    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();
    }
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)