winforms 如何在Windows应用程序C#中使用凭据从URL下载文件

pkbketx9  于 2022-11-16  发布在  Windows
关注(0)|答案(2)|浏览(213)

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

  • 链接到文件
  • 保存本地磁盘路径的路径
  • 使用登录
  • 口令
unftdfkk

unftdfkk1#

我给予大家举个例子:
其核心思想是:单击下载后验证帐户密码,如果正确则下载。
使用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


指令集

jaql4c8m

jaql4c8m2#

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

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

相关问题