简体   繁体   English

通过从控制台应用程序C#登录,从网站的导航链接下载文件。

[英]download the file from the navigation link from the website by logging in from Console application C#

I would like to download excel file from the website using user name and password. 我想使用用户名和密码从网站下载excel文件。 After logging in, there is a link for downloading the excel file. 登录后,有用于下载excel文件的链接。 When I clicked it, the Save as message asks me to save in the desired folder. 当我单击它时,另存为消息要求我保存在所需的文件夹中。 I am using this function and it doesn't work. 我正在使用此功能,因此无法正常工作。 It just downloads the html page. 它只是下载html页面。 Any suggestions would be appreciated. 任何建议,将不胜感激。

ftpfullpath is the url + file name ftpfullpath是url +文件名

if I use the login form url, it shows error. 如果我使用登录表单网址,则会显示错误。

    public static void DownloadFileFTP()
        {
            //Delete if Data feed file is already existed
            if (File.Exists(GetFileName.source_path))
            {
                File.Delete(GetFileName.source_path);
            }

            //Create a WebClient
            WebClient request = new WebClient();

            //Set up credentials
            request.Credentials = new NetworkCredential(Properties.Resources.UserName, Properties.Resources.Password);

            //Download the data into Byte array
            byte[] fileData = request.DownloadData(Properties.Resources.ftpfullpath);

            //Create a Filestream to write to Byte array
            FileStream sourceFile = File.Create(GetFileName.source_path);

            try
            {
                //Write full byte array to the file
                sourceFile.Write(fileData, 0, fileData.Length);
                Console.WriteLine("Download Complete " + GetFileName.source_path);
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }

            finally
            {
                if (sourceFile != null)
                {
                sourceFile.Close();
                sourceFile = null;
                }

            }
        }

Your code is ok, you have to debug page you're downloading from. 您的代码正常,您必须调试要从中下载的页面。 Most likely it makes redirects before going to file. 最有可能在进行归档之前进行重定向。 Browser handles that automatically. 浏览器会自动处理。 Use http://www.fiddler2.com/fiddler2/ to see what's happening when you click the link. 使用http://www.fiddler2.com/fiddler2/查看单击链接时发生的情况。

Also try to set referrer = download page url. 另外,尝试设置引荐来源网址=下载页面网址。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM