【问题标题】:How to download a file from URL with credentials in windows application C#如何在 Windows 应用程序 C# 中使用凭据从 URL 下载文件
【发布时间】:2021-11-18 21:02:54
【问题描述】:

大家好!我需要一个从网站下载文件的功能,在下载之前它将被重定向到登录页面。所以我也必须验证自己。 该功能将在桌面上的窗口应用程序中使用。 函数参数:

  • 文件链接
  • 将路径保存到本地磁盘的路径
  • 使用登录
  • 密码

【问题讨论】:

  • 我已经做出了回答。如果您不介意,可以点击“✔”将我的回复标记为已接受答案。它还将帮助其他人解决类似的问题。
  • 证明自己是什么意思?需要输入账号密码,还是直接用验证码登录?
  • 请提供足够的代码,以便其他人更好地理解或重现问题。

标签: c# visual-studio winforms download desktop-application


【解决方案1】:

我给你举个例子:

核心思路是:点击下载后验证账号密码,正确则下载。

使用DialogResult返回验证结果。

使用 SaveFileDialog 选择要保存的位置。

在文本框中输入您要下载的 URL。

主窗口代码:

using System;
using System.Net;
using System.Windows.Forms;

namespace WindowsFormsApp1 {
    public partial class Form1 : Form {
        public Form1() {
            InitializeComponent();
        }
        private void Button1_Click(object sender, EventArgs e) {
            login f2 = new login();
            while (true) {
                f2.ShowDialog();
                if (f2.DialogResult == DialogResult.OK) {
                    Download();
                } else if (f2.DialogResult == DialogResult.Cancel)
                    MessageBox.Show("Verification Failed");
                break;
            }
            f2.Close();
        }

        //Download function
        public void Download() {
            SaveFileDialog sfd = new SaveFileDialog();
            //Set the title of the save file dialog
            sfd.Title = "Please select the file path to save.";
            //Initialize the save directory, the default exe file directory
            sfd.InitialDirectory = Application.StartupPath;
            //Set the type of saved file
            sfd.Filter = "Text file|*.txt|Audio file|*.wav|Picture file|*.jpg|All files|*.*";
            if (sfd.ShowDialog() == DialogResult.OK) {
                //Get the path to save the file
                string filePath = sfd.FileName;
                //save
                try {
                    WebClient client = new WebClient();
                    client.DownloadFile(textBox1.Text, filePath);
                } catch (WebException webEx) {
                    Console.Write(webEx.ToString());
                }
            }
        }
     }
}

登录窗口代码:

using System;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace WindowsFormsApp1 {
    public partial class login : Form {
        public login() {
            InitializeComponent();
        }

        private void Button1_Click(object sender, EventArgs e) {
            //Personal test database
            string myconn = @"Data Source = (localdb)\MSSQLLocalDB; Initial Catalog = Test; Integrated Security = True";
            SqlConnection conn = new SqlConnection(myconn);     
            string sql= $"select * from Test.dbo.demoAccount where userid='{ AccountTb.Text}' and password='{PassTb.Text}'";
            conn.Open();
            SqlCommand sqlCommand = new SqlCommand(sql, conn);
            SqlDataReader sqlDataReader = sqlCommand.ExecuteReader();
            if (sqlDataReader.HasRows)//Satisfy the user name and password are consistent, enter the next interface
            {
                this.DialogResult = DialogResult.OK;
            } else {
                this.DialogResult = DialogResult.Cancel;
            }
            conn.Close();
}
    }
}

正确操作示意图:

Test Url

【讨论】:

  • 对你有帮助吗?
  • 抱歉回复晚了。感谢您的详细回复,但您似乎正在从自己的数据库中验证凭据,但在我的场景中,凭据必须由网站验证(不是我所有的)。
  • 抱歉误解了你的意思。你的意思是在代码中自动登录?
  • 您可以使用httpwebrequest登录目标网站。如果可能,请提供目标登录表单的帐号和密码。
  • 能否给我一些代码sn-ps,以便更好地帮助您解决问题?
【解决方案2】:

使用WebClient 工具怎么样? 您可以设置凭据并将响应写入文件。

    try {
        WebClient client = new WebClient();
        client.Credentials = new NetworkCredential("username", securelyStoredPwd);
        client.DownloadFile(linkToTheFile,pathToTheFile);   
    } catch (WebException webEx) {
        Console.Write(webEx.ToString());
    }

【讨论】:

    猜你喜欢
    • 2018-04-16
    • 2015-04-09
    • 1970-01-01
    • 2017-01-10
    • 1970-01-01
    • 2011-01-30
    • 2011-09-17
    • 1970-01-01
    • 2015-03-04
    相关资源
    最近更新 更多