Mohamad MohamadPoor
Mohamad MohamadPoor

Reputation: 1340

Download A Non-Directed File With C# In WPF

I have a small problem and I can not figure out how to solve my problem. I need to download a file from a site in my WPF application, but my problem is i can't access direct link of file because the file is updated daily and the URL respond different file each day. link is correct and i can download in IDM or any web browser, but when I use C# WPF classes like WebClient, it download something else and when I open the file with office it says that file is corrupted. can anyone offer me how to download my excel file from this link using C# in WPF?

Here is the link

Also another problem is I don't know respond file's name, is it possible to figure out the file name too ?

I will appreciate any response, thanks a lot.

Upvotes: 0

Views: 728

Answers (2)

Mike Hixson
Mike Hixson

Reputation: 5189

From the looks of it, the server is not respecting the Accept-Encoding header sent by the request. It just always sends the response with gzip encoding. I was able to download the file sucessfully with HttpClient, once I set AutomaticDecompression to GZip.

static void Main()
{
    var task = DownloadFileAsync("http://members.tsetmc.com/tsev2/excel/MarketWatchPlus.aspx?d=0");
    task.Wait();

}

static async Task DownloadFileAsync(string url)
{
    HttpClient client = new HttpClient(new HttpClientHandler { AutomaticDecompression = DecompressionMethods.GZip });

    HttpResponseMessage response = await client.GetAsync(url);

    // Get the file name from the content-disposition header.
    // This is nasty because of bug in .net: http://stackoverflow.com/questions/21008499/httpresponsemessage-content-headers-contentdisposition-is-null
    string fileName = response.Content.Headers.GetValues("Content-Disposition")
        .Select(h => Regex.Match(h, @"(?<=filename=).+$").Value)
        .FirstOrDefault()
        .Replace('/', '_');

    using (FileStream file = File.Create(fileName))
    {
        await response.Content.CopyToAsync(file);
    }
}

Upvotes: 2

Chris
Chris

Reputation: 5514

I had a look at the http headers that the server sends back:

Content-Encoding: gzip
Vary: *
Content-Disposition: attachment; filename=MarketWatchPlus-1393/4/25.xlsx
Content-Length: 77100
Cache-Control: public, max-age=60
Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet
Date: Tue, 15 Jul 2014 20:59:46 GMT
Expires: Tue, 15 Jul 2014 21:00:44 GMT
Last-Modified: Tue, 15 Jul 2014 20:59:44 GMT
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET

Notice how it says gzip there. I renamed the file to .gz and unzipped it, looks fine then. Looks that while web browsers sort that out by themselves, the WebRequest classes do not.

To answer your second question, the file name is in the headers as well.

Upvotes: 2

Related Questions