下载一个文件,但似乎我必须用浏览器来做 [英] Download a file but it seems like I have to do it with a browser

查看:28
本文介绍了下载一个文件,但似乎我必须用浏览器来做的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从网站下载文件,但除非加载不同的页面,否则特定 URL 不起作用.

I would like to download a file from a website but the specific URL does not work unless a different page is loaded.

我实际上是调用 Web 浏览器来加载页面,然后调用另一个页面来下载文件.

I literally call a web browser to load a page and then call another page to download a file.

这有效,但我讨厌它...

This works but I hate it...

static void Main(string[] args)
        {
            //Open website
            Process.Start("chrome.exe", "https://www.fapiis.gov/fapiis/allfapiisdata.action");

            //Wait 20 sec
            System.Threading.Thread.Sleep(20000);

            //DownLoad File
            Process.Start("Chrome.exe", "https://www.fapiis.gov/fapiis/downloadview?type=allFapiis");

            //Wait 10 sec
            System.Threading.Thread.Sleep(10000);

            //Close Chrome
            Process[] chromeInstances = Process.GetProcessesByName("chrome");
            foreach (Process p in chromeInstances)
            { p.Kill(); }

            //Move file for SSIS Processing
            string[] files = System.IO.Directory.GetFiles(@"C:\Users\xxxx\Downloads", "AllFapiis*");
            foreach (string file in files)
            {
                string fname = System.IO.Path.GetFileName(file);
                System.IO.File.Copy(file, @"C:\xxxxx\FAPIIS\" + fname);
                System.IO.File.Delete(file);
            }
        }

我尝试使用 webclient,但它永远找不到第二页.还有其他方法可以在不调用浏览器的情况下做到这一点吗?另一个问题是我无法指示下载的位置.它会自动进入我的用户帐户下载.

I tried using webclient but it can never find the 2nd page. Any other way to do this without calling a browser? Another problem is I can't direct where the download goes. It automatically goes into downloads on my user account.

推荐答案

您尝试下载的站点使用 cookie.因此,一旦您导航到第一页,就会在浏览器中设置一些 cookie.这允许在第二页上下载.

The site you are trying to download from, uses cookies. So once you navigate to the first page, some cookies are set to the browser. This allows the download on the second page.

您可以使用以下代码完成此操作:

You could accomplish this with this code:

static async Task TryDownload()
{
    var clientHandler = new HttpClientHandler
    {
        AllowAutoRedirect = true,
        UseCookies = true,
        CookieContainer = new CookieContainer()
    };

    using (var httpClient = new HttpClient(clientHandler))
    {
        // this gets the request and allows the site to set cookies.
        var warmup = await httpClient.GetAsync("https://www.fapiis.gov/fapiis/allfapiisdata.action");

        // get the file (cookies are sent automatically).
        var fileResponse = httpClient.GetAsync("https://www.fapiis.gov/fapiis/downloadview?type=allFapiis");

        if (fileResponse.Result.IsSuccessStatusCode)
        {
            HttpContent content = fileResponse.Result.Content;
            var contentStream = await content.ReadAsStreamAsync();
            using (var fileStream = File.Create("C:\\temp\\a.xlsx"))
            {
                contentStream.CopyTo(fileStream);
            }
        }
    }
}

输出:

Excel file in c:\temp (662,044 bytes)

这篇关于下载一个文件,但似乎我必须用浏览器来做的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆