gpt4 book ai didi

c# - 以编程方式自动化 Web 登录

转载 作者:太空宇宙 更新时间:2023-11-03 17:59:57 27 4
gpt4 key购买 nike

我正在尝试创建一个 C# Winforms 应用程序,它会自动将我登录到站点并下载数据。具体来说,我想让我的应用程序自动登录我的网上银行网站,让我登录并下载我的交易历史记录。我可以通过网络浏览器登录并下载它来手动执行此操作。我正在尝试自动化这个。我知道我可能需要使用 HttpWebRequest 和 HttpWebResponse。有没有人有这方面的例子或我需要采取的步骤框架?请记住,这将是安全站点 (https),​​我将不得不以某种方式收集 session 信息并在 session 期间保留 session 信息。有什么想法吗?

最佳答案

using System;

using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;

namespace testSSL
{
public partial class FormDownload : Form
{
private bool success;
private const string filename = "file.txt";
private const string url_string = "https://some.url.com";
private Uri url;
public FormDownload()
{
InitializeComponent();
success = false;
url = new Uri(url_string);
}

public bool StartDownload()
{
this.ShowDialog();
return success;
}

private void Form1_Load(object sender, EventArgs e)
{
this.Activate();

progressBar1.Maximum = 100;
label1.Text = "Working";

WebClient client = new WebClient();
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.DownloadFileCompleted += new AsyncCompletedEventHandler(client_DownloadFileCompleted);

//possible fix for running on w2k
ServicePointManager.SecurityProtocol = SecurityProtocolType.Ssl3;

string user="user", pass="pass";
client.Credentials = new NetworkCredential(user, pass);
try
{
client.DownloadFileAsync(url, filename);
}
catch (Exception ue)
{
writeException(ue.Message);
}

}

void client_DownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error != null)
{
writeException(e.Error.Message);
success = false;
}
else
{
label1.Text = "Done";
System.Threading.Thread.Sleep(100);
success = true;
}
this.Close();
}

void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}

private void writeException(string ex)
{
ex = "Date: " + DateTime.Now.ToString() + " Exception: " + ex + "\r\n";
File.AppendAllText("downloadLog.txt", ex);
MessageBox.Show("An error has occurred; it has been logged");
this.Close();
}
}

}

关于c# - 以编程方式自动化 Web 登录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2917126/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com