1

我正在开发一个概念证明程序,该程序执行 webrequestWebClient.DownloadString("http://website/members/login.php?user=" + textBox1.Text + "&pass=" + textBox2.Text); 以获取用户是否是有效登录名的布尔值,然后如果是,它会给出成功通知,如果不是十则给出失败通知。

问题是当我第一次按下按钮尝试登录时它工作正常,但是当我再次按下它时,程序冻结并卡在 Webclient.download 字符串中。

如果有人能发现并告诉我出了什么问题,那就太好了。我提供以下代码:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text;
using System.Net;
using System.IO;
using System.Diagnostics;
using System.Net;
using System.Collections;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        public static WebClient webclient = new WebClient();

        HttpWebResponse wResp;
        WebRequest wReq;
        bool isConnected = false;
        private String Session = "";

        public Form1()
        {
            InitializeComponent();
        }

        public Boolean checkUser(String username, String password)
        {
                String login = `webclient.DownloadString("http://connorbp.info/members/auth.php?user=" + textBox1.Text + "&pass=" + textBox2.Text);`
                Boolean bLogin = Boolean.Parse(login);
                if (bLogin)
                {
                    Session = username + "-" + password;
                }
                return bLogin;
        }

        public int CanConnect(string dUrl)
        {
            wReq = WebRequest.Create(dUrl);
            int cnt = Connect();
            return cnt;
        }

        private int Connect()
        {
            try
            {
                wResp = (HttpWebResponse)wReq.GetResponse();
                isConnected = true;
                return 1;
            }
            catch (Exception)
            {
                return 0;
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int init = CanConnect("http://connorbp.info/members/auth.php");
            if (init == 0)
            {
                notifyIcon1.ShowBalloonTip(200, "CBP Login", "Failed to connect to server! Try again later.", ToolTipIcon.Error);
            }
            else
            {
                if(checkUser(textBox1.Text, textBox2.Text))
                {
                    notifyIcon1.ShowBalloonTip(20, "CBP Login", "Logged In!", ToolTipIcon.Info);
                }
                else
                {
                    notifyIcon1.ShowBalloonTip(20, "CBP Login", "Invalid Username/Password!", ToolTipIcon.Error);
                }
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.MaximizeBox = false;
            notifyIcon1.ShowBalloonTip(20, "CBP Login", "for more cool things go to http://connorbp.info", ToolTipIcon.Info);
        }
    }
}
4

1 回答 1

3

您没有关闭响应。

第二个调用试图打开已经打开的东西,因此它挂起。

于 2012-11-11T22:10:32.070 回答