简体   繁体   中英

C# Program stops responding after second WebClient.DownloadString()

I am working on a program for proof of concept that does a webrequest using WebClient.DownloadString("http://website/members/login.php?user=" + textBox1.Text + "&pass=" + textBox2.Text); to get the boolean value of wether or not the user is a valid login and then if it is it gives a success notification if it isn't ten it gives a fail notification.

The problem is when i press the button to try and login the first time it works fine but when i press it again the second tine the program freezes and gets stuck at the Webclient.download string.

If anyone can spot and tell me whats wrong that would be great. I am providing the code below:

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

You are not closing the response.

The second call is trying to open something that is already open, therefore it hangs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM