我试图使用网络浏览器控制自动登录到一个网站论坛,但它永远不会登录什么可能是错误的
本文关键字:登录 永远 网站 论坛 错误 什么 一个 网络 浏览器 控制 | 更新日期: 2023-09-27 18:10:39
这是登录的链接:
登录如果我试图在web浏览器手动登录,它什么也不做,但打开一个弹出消息问我是否要关闭窗口,我点击是,什么都没有发生。在普通的chrome浏览器,而不是在web浏览器中,当我输入用户名和密码,并点击登录,它也关闭了窗口的登录自动刷新页面,我登录。但是通过浏览器它永远不会登录。它不给出任何错误或异常,只是什么也不做。
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.Net;
using System.IO;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Security.Permissions;
namespace Champinos
{
public partial class Form1 : Form
{
WebClient webc = new WebClient();
string url = "";
string username = "";
string password = "";
string commit = ""; //this matches the data from Tamper Data
private int timeElapsed;
private string newline1 = "";
private bool complete;
private int count;
public Form1()
{
InitializeComponent();
timeElapsed = 1;
label3.Visible = false;
label5.Visible = false;
complete = false;
count = 1;
}
void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
url = "http://www.tapuz.co.il/Common/SignInPage.aspx?backUrl=http://www.tapuz.co.il/Common/SignIn.aspx@loginDone=1";
username = "yasonikolados";
password = "ynnad1972";
commit = "Login";//"Sign+In";
label1.Text = "Completed";
string source = "";
source = webc.DownloadString("http://www.tapuz.co.il/forums2008/forumpage.aspx?forumid=393&r=1");
string start = "data-token=";
string end = " href";
int firstTag = source.IndexOf(start);
int lastTag = source.IndexOf(end, firstTag);
int startIndex = firstTag + start.Length + 1;
int endIndex = lastTag;
string authenticityToken = source.Substring(startIndex, endIndex - startIndex - 30);
webBrowser1.DocumentCompleted -= webBrowser1_DocumentCompleted;
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted2;
string postData = string.Format(
"authenticity_token={2}&session" +
"[username_or_email]={0}&session[password]={1}&commit={3}",
username, password, authenticityToken, commit);
ASCIIEncoding enc = new ASCIIEncoding();
webBrowser1.Navigate("http://www.tapuz.co.il/Common/SignInPage.aspx?backUrl=http://www.tapuz.co.il/Common/SignIn.aspx@loginDone", "", enc.GetBytes(postData), "Content-Type: application/x-www-form-urlencoded'r'n");
}
private void Form1_Load(object sender, EventArgs e)
{
webBrowser1.ScriptErrorsSuppressed = true;
webBrowser1.DocumentCompleted += webBrowser1_DocumentCompleted;
webBrowser1.Navigate("http://www.tapuz.co.il/forums2008/forumpage.aspx?forumid=393&r=1");
}
private void webBrowser1_DocumentCompleted2(object sender, WebBrowserDocumentCompletedEventArgs e)
{
WebBrowser b = sender as WebBrowser;
string response = b.DocumentText;
if (response.Contains("Sign out"))
{
MessageBox.Show("Login Successful");
}
}
}
}
这是因为您使用webBrowser1.Navigate
作为DocumentCompleted
事件处理程序中的最后一个调用。由于您在Navigate
调用之前添加了webBrowser1_DocumentCompleted2
作为DocumentCompleted
事件的事件处理程序,因此正在发生的事情是,当对导航的调用发生时,它会立即调用您的DocumentCompleted2
函数,该函数正在将您签出。即时登录和注销。
如果您想确认,请在DocumentCompleted
和DocumentCompleted2
函数的第一行设置断点并进行调试。