通过txt文件读取凭据,然后尝试登录c#

本文关键字:然后 登录 txt 文件 读取 通过 | 更新日期: 2023-09-27 18:22:29

我会为函数创建一个循环,并在catch中为所有失败的登录打印消息,并为所有正确的登录打印"successful connected with…"。现在我尝试使用该代码,但在catch 的文本框中只得到一个错误

按钮1:

  if (openFileDialog1.FileName != string.Empty)
                {
                    using (StreamReader reader = new StreamReader(openFileDialog1.FileName))
                    {
                        int count = 0;
                        string lineoflistemail;
                        while ((lineoflistemail = reader.ReadLine()) != null)
                        {
                            UserData d = new UserData();
                            string[] parts = lineoflistemail.Split(':');
                            count = parts.Length;
                            d.UserName = parts[0].Trim();
                            d.Password = parts[1].Trim();
                            data.Add(d);
                        }
                        foreach(UserData ud in data)
                        {
                            textBox1.Text += ("LOL" + ud.UserName + ud.Password + Environment.NewLine);
                        }

第二个按钮代码:

if (data.Count() == 0)
            {
                MessageBox.Show("Load user info first");
                return;
            }
            for( hola = 0; hola < data.Count(); hola++) 
            {

            var url = @"https://mail.google.com/mail/feed/atom";
            var encoded = TextToBase64(data[0].UserName + ":" + data[1].Password);
            var myweb = HttpWebRequest.Create(url) as HttpWebRequest;
            myweb.Method = "POST";
            myweb.ContentLength = 0;
            myweb.Headers.Add("Authorization", "Basic " + encoded);
            var response = myweb.GetResponse();
            var stream = response.GetResponseStream();
            textBox1.Text += ("Connection established with");
            MessageBox.Show(hola.ToString());
        }
        }
        catch (Exception ex)
        {
            textBox1.Text += ("Error connection. Original error: " + ex.Message);
        }
        }

通过txt文件读取凭据,然后尝试登录c#

问题是try...catchfor循环之外。第一个异常将退出for循环,将消息附加到textBox1,然后退出按钮处理程序。

如果即使出现错误也要保持循环,请将try...catch移动到循环内部。这里有一个例子:

for( hola = 0; hola < data.Count(); hola++) 
{
    var url = @"https://mail.google.com/mail/feed/atom";
    var encoded = TextToBase64(data[0].UserName + ":" + data[1].Password);
    var myweb = HttpWebRequest.Create(url) as HttpWebRequest;
    myweb.Method = "POST";
    myweb.ContentLength = 0;
    myweb.Headers.Add("Authorization", "Basic " + encoded);
    try
    {
        var response = myweb.GetResponse();
        var stream = response.GetResponseStream();
        textBox1.Text += ("Connection established with");
        MessageBox.Show(hola.ToString());
    }
    catch (Exception ex)
    {
        textBox1.Text += ("Error connection. Original error: " + ex.Message);
    }
}
相关文章: