簡體   English   中英

通過從控制台應用程序C#登錄,從網站的導航鏈接下載文件。

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

我想使用用戶名和密碼從網站下載excel文件。 登錄后,有用於下載excel文件的鏈接。 當我單擊它時,另存為消息要求我保存在所需的文件夾中。 我正在使用此功能,因此無法正常工作。 它只是下載html頁面。 任何建議,將不勝感激。

ftpfullpath是url +文件名

如果我使用登錄表單網址,則會顯示錯誤。

    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;
                }

            }
        }

您的代碼正常,您必須調試要從中下載的頁面。 最有可能在進行歸檔之前進行重定向。 瀏覽器會自動處理。 使用http://www.fiddler2.com/fiddler2/查看單擊鏈接時發生的情況。

另外,嘗試設置引薦來源網址=下載頁面網址。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM