繁体   English   中英

如何自动登录此网页

[英]how to log in to this web page automatically

http://www.conquerclub.com/game.php?game=13025037

请问有谁知道如何以编程方式登录到这个地址,然后将所有页面的html作为字符串返回使用类似HttpWebRequest / HttpWebResponse的内容。

用户名 - “testuser1”

密码 - “测试”

(我已使用这些凭据在网站上创建了一个帐户,因此它实际上会登录)

到目前为止我已经有了这个

private void toolStripButton1_Click(object sender, EventArgs e)
{
    var request = WebRequest.Create("http://www.conquerclub.com/game.php?game=13025037");
    HttpWebResponse response = (HttpWebResponse)request.GetResponse();
    StreamReader reader = new StreamReader(response.GetResponseStream());
    richTextBox1.Text = reader.ReadToEnd();
}

这会自动重定向并返回登录页面的html。 如何通过用户名和密码自动登录,以便检索游戏页面?

我知道在这方面有很多类似的问题,我花了几个小时尝试不同的事情,但无法得到任何工作。

编辑:-

进一步查看此网站使用cookie登录。

试过这段代码,但仍然只是返回登录页面。

private void toolStripButton1_Click(object sender, EventArgs e)
{
    string loginUri = "http://www.conquerclub.com/login.php";
    string username = "testuser1";
    string password = "testing";
    string reqString = "username=" + username + "&password=" + password;
    byte[] requestData = Encoding.UTF8.GetBytes(reqString);

    CookieContainer cc = new CookieContainer();
    var request = (HttpWebRequest)WebRequest.Create(loginUri);
    request.Proxy = null;
    request.AllowAutoRedirect = false;
    request.CookieContainer = cc;
    request.Method = "post";
    request.ContentType = "application/x-www-form-urlencoded";
    request.ContentLength = requestData.Length;
    using (Stream s = request.GetRequestStream())
        s.Write(requestData, 0, requestData.Length);

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        if (response.Cookies != null)
        {
            foreach (Cookie c in response.Cookies)
                Console.WriteLine(c.Name + " = " + c.Value);
        }
    }

    string newloginUri = "http://www.conquerclub.com/game.php?game=13025037";
    HttpWebRequest newrequest = (HttpWebRequest)WebRequest.Create(newloginUri);
    newrequest.Proxy = null;
    newrequest.CookieContainer = cc;
    using (HttpWebResponse newresponse = (HttpWebResponse)newrequest.GetResponse())
    using (Stream resSteam = newresponse.GetResponseStream())
    using (StreamReader sr = new StreamReader(resSteam))
        richTextBox1.Text = sr.ReadToEnd();
}

已经发现登录游戏页面的底部代码可以通过首先手动登录而在firefox上使用fiddler然后复制并粘贴cookie并将其硬编码到这样的新请求中。

string newloginUri = "http://www.conquerclub.com/game.php?game=13025037";
HttpWebRequest newrequest = (HttpWebRequest)WebRequest.Create(newloginUri);
newrequest.Proxy = null;
newrequest.CookieContainer = new CookieContainer();
newrequest.CookieContainer.Add(new Uri("http://www.conquerclub.com"), new Cookie("PHPSESSID","86bte1ipainiq760vm2flv4h13"));
newrequest.CookieContainer.Add(new Uri("http://www.conquerclub.com"), new Cookie("phpbb3_jer7c_u", "648113"));
newrequest.CookieContainer.Add(new Uri("http://www.conquerclub.com"), new Cookie("phpbb3_jer7c_k", ""));
newrequest.CookieContainer.Add(new Uri("http://www.conquerclub.com"), new Cookie("phpbb3_jer7c_sid", "3eebb0771a68c4a58581e495c34b2c93"));
using (HttpWebResponse newresponse = (HttpWebResponse)newrequest.GetResponse())
using (Stream resSteam = newresponse.GetResponseStream())
using (StreamReader sr = new StreamReader(resSteam))
    richTextBox1.Text = sr.ReadToEnd();

这将返回我想要的游戏页面,但无法弄清楚如何使登录工作,以便它将返回正确的cookie。 在调试代码时,返回的cookie与我在fiddler中看到的完全不同,所以看起来它只是没有登录。

您编写的代码执行了给定URL的GET; 但是,要在登录时检索页面内容,您需要假装您的WebRequest实际填写表单,方法是传入所有表单变量并发出POST请求。

http://msdn.microsoft.com/en-us/library/debx8sh9.aspx详细介绍了您需要的步骤。 以下内容并非100%完成,但应该为您指明正确的方向:

var request = WebRequest.Create("http://www.conquerclub.com/login.php");
request.Method = "POST";

var parameters = new Dictionary<string, string> { 
    { "username", "testuser1" }, 
    { "password", "testing" }, 
    { "redirect", "game.php?game=13025037" }, 
    { "direct", "63###www.conquerclub.com###" }, 
    { "protocol", "HTTP" }, 
    { "submit", "Login" } };

var content = string.Join( "&", ( from p in parameters select p.Key + "=" + HttpUtility.UrlEncode( p.Value) ).ToArray() ); ;

byte[] bytes = new byte[content.Length * sizeof(char)];
System.Buffer.BlockCopy(content.ToCharArray(), 0, bytes, 0, bytes.Length);

request.ContentLength = bytes.Length;
request.ContentType = "application/x-www-form-urlencoded";

Stream dataStream = request.GetRequestStream();
dataStream.Write( bytes, 0, bytes.Length );
dataStream.Close();

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string result = reader.ReadToEnd();

请注意, content包括屏幕上不可见的各种值; 通过使用所选浏览器调试工具栏上的“网络”选项卡并监视提交表单时发送的数据,可以查看表单的源,或(更简单)。

您需要模仿登录过程。查看登录页面的HTML代码,并找出三件事:

  1. 发送请求的<form> ,特别是<form>的目标(在属性action找到)。
  2. 找出发送数据的方法(在属性method找到)
  3. 找到代表用户名和密码的字段的ID。

拥有那些你可以轻松构建模仿登录的WebRequest。

请注意,这假设是直接登录屏幕(没有AJAX,没有CAPTCHA)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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