C#程序在第二个WebClient.DownloadString()后停止响应

时间:2012-11-11 22:03:59

标签: c# web-services webclient downloadstring

我正在开发一个使用WebClient.DownloadString("http://website/members/login.php?user=" + textBox1.Text + "&pass=" + textBox2.Text);进行网络请求的概念验证程序 要获取或不是用户的布尔值,用户是有效的登录,然后如果是,它会给出成功通知,如果它不是10,则会发出失败通知。

问题是当我按下按钮尝试登录时它第一次工作正常但是当我再次按下它时第二个时间程序会冻结并卡在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);
        }
    }
}

1 个答案:

答案 0 :(得分:3)

您没有关闭回复。

第二个电话试图打开已经打开的东西,因此它会挂起。