2012-11-11 32 views
1

我的程序工作的概念,不使用WebClient.DownloadString("http://website/members/login.php?user=" + textBox1.Text + "&pass=" + textBox2.Text); 得到羯羊或沒有用戶的布爾值的WebRequest證明是有效的登錄,然後,如果它它是否提供成功通知,如果它不是十則它會發出失敗通知。C#程序停止第二WebClient.DownloadString()之後響應

問題是,當我按下按鈕,嘗試和登錄第一次它工作正常,但是當我再次按下第二齒程序凍結並卡在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

您不會出現在'Connect'法期間關閉你的'WebRequest'。由於這是一個成員變量,我會嘗試首先妥善處理。 –

+0

我終於想通了!我正在讀這個錯誤,這是Connect方法中的響應。謝謝:) – connorbp

回答

3

您沒有關閉響應。

第二個電話試圖打開一些已經打開,因此它掛起。

+0

我會試試這個,我正在思考沿線的東西,但不知道:) – connorbp

+0

我似乎無法找到如何在任何地方關閉響應。到處看的代碼只是沒有了downloadstring然後退出(控制檯應用程序) – connorbp