Home > Enterprise >  How to download a file from URL with credentials in windows application C#
How to download a file from URL with credentials in windows application C#

Time:09-28

Hy guys! I need a function to download a file from a website, before downloading it will be redirected to login page. So I have to authenticate myself as well. The function will be used in the window app on a desktop. Function arguments:

  • link to the file
  • path to save a path to local disk
  • use login
  • password

CodePudding user response:

What about using the enter image description here

Login window code:

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

enter image description here

Schematic diagram of correct operation:

enter image description here

enter image description here

enter image description here

enter image description here

  • Related