如何自动登录此网页

时间:2013-07-25 22:47:49

标签: c# login httpwebrequest

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中看到的cookie完全不同,所以看起来它只是没有登录。

2 个答案:

答案 0 :(得分:2)

您编写的代码执行了给定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包含屏幕上不可见的各种值;通过查看表单的源代码可以看到这些,或者(更简单)通过使用所选浏览器调试工具栏上的“网络”选项卡并监视提交表单时发送的数据。

答案 1 :(得分:0)

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

  1. 发送请求的<form>,特别是表单中的目标(在属性action中找到)。
  2. 找出发送数据的方法(在属性method中找到)
  3. 找到代表用户名和密码的字段的ID。
  4. 有了那些你可以轻松构建模仿登录的WebRequest。

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