【问题标题】:C# Program stops responding after second WebClient.DownloadString()C# 程序在第二个 WebClient.DownloadString() 后停止响应
【发布时间】:2012-11-12 06:10:32
【问题描述】:

我正在开发一个概念证明程序,该程序使用WebClient.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);
        }
    }
}

【问题讨论】:

  • 您似乎没有在Connect 方法期间关闭您的WebRequest。由于这是一个成员变量,我会先尝试正确处理它。
  • 我终于想通了!我读错了,这是 Connect 方法中的响应。谢谢:)

标签: c# web-services webclient downloadstring


【解决方案1】:

您没有关闭响应。

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

【讨论】:

  • 我会试试这个,我在想类似的东西,但不确定:)
  • 我似乎无法找到如何在任何地方关闭响应。我到处看代码只是下载字符串然后退出(控制台应用程序)
猜你喜欢
  • 2021-11-27
  • 1970-01-01
  • 2017-11-20
  • 2023-03-03
  • 2019-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多