C# 如何自动登录此网页
请问有谁知道我如何通过编程登录到这个地址,然后使用类似于HttpWebRequest/HttpWebResponse的东西将页面的所有html作为字符串返回 用户名-“testuser1” 密码-“测试” (我已使用这些凭据在网站上创建了一个帐户,因此它将实际登录) 到目前为止我已经知道了C# 如何自动登录此网页,c#,login,httpwebrequest,C#,Login,Httpwebrequest,请问有谁知道我如何通过编程登录到这个地址,然后使用类似于HttpWebRequest/HttpWebResponse的东西将页面的所有html作为字符串返回 用户名-“testuser1” 密码-“测试” (我已使用这些凭据在网站上创建了一个帐户,因此它将实际登录) 到目前为止我已经知道了 private void toolStripButton1_Click(object sender, EventArgs e) { var request = WebRequest.Create("h
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。如何传递用户名和密码以自动登录,使其检索游戏页面
我知道在这个问题上有很多类似的问题,我花了好几个小时尝试不同的东西,但什么都没用
编辑:-
对此进行了进一步研究,该网站使用cookies进行登录
已尝试此代码,但仍只是返回登录页
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时,首先手动登录,然后复制并粘贴cookies,并将其硬编码为newrequest,就可以让底部登录游戏页面的代码正常工作
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();
这会返回我想要的游戏页面,但无法确定如何让登录正常工作,以便返回正确的cookies。在调试代码时,返回的cookies与我在fiddler中看到的cookies完全不同,因此看起来它只是没有登录。您需要模拟登录过程。查看登录页面的HTML代码,找出三件事:
,特别是表单的目标(在属性操作中找到)
方法中找到)
请注意,这假设一个直接的登录屏幕(没有AJAX,没有CAPTCHA)。您编写的代码会获取给定的URL;但是,要在登录时检索页面内容,您需要假装WebRequest实际上正在填写表单,方法是传入所有表单变量并发出POST请求 提供所需步骤的详细演练。以下内容并非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();
var request=WebRequest.Create(“http://www.conquerclub.com/login.php");
request.Method=“POST”;
var参数=新字典{
{“用户名”,“testuser1”},
{“密码”,“测试”},
{“重定向”,“game.php?game=13025037”},
{“direct”、“63############www.conquenclub.com},
{“协议”,“HTTP”},
{“提交”、“登录”};
var content=string.Join(“&”,(从参数中的p选择p.Key+“=”+HttpUtility.UrlEncode(p.Value)).ToArray();
字节[]字节=新字节[content.Length*sizeof(char)];
System.Buffer.BlockCopy(content.ToCharArray(),0,bytes,0,bytes.Length);
request.ContentLength=字节.Length;
request.ContentType=“application/x-www-form-urlencoded”;
Stream dataStream=request.GetRequestStream();
dataStream.Write(字节,0,字节.长度);
dataStream.Close();
HttpWebResponse=(HttpWebResponse)request.GetResponse();
StreamReader=新的StreamReader(response.GetResponseStream());
字符串结果=reader.ReadToEnd();
请注意,
内容
包括屏幕上看不到的各种值;查看表单的源代码,或者(更容易)使用浏览器调试工具栏上的“网络”选项卡,并在提交表单时监视发送的数据,就可以看到这些信息。最简单的方法是手动登录,并使用Fiddler查看发送的请求。然后可以在代码中重新构造该请求。(注意:看起来他们没有使用SSL,所以你不必在Fiddler中启用任何HTTPS功能,但如果需要的话,你可以在将来参考。)是的,我现在可以让它直接登录到游戏页面,但只需从Fiddler复制粘贴cookies并硬编码即可。我不知道如何让它自动登录并检索相关的cookies。这是从页面源粘贴的-登录用户名密码-我不知道如何从中构造WebRequest。@Oli-然后是时候做一些家庭作业和研究了:这是一种可能性,尽管它似乎花费了很多时间学习一个全新的话题只是为了做一件事。我真的希望有人能告诉我怎么做。@Oli-欢迎来到编程。这就是你学习东西并成为一名更好的程序员的方式。你可能会也可能不会再次使用这些知识,但通过学习,你已经成为了一名更好的编码员。非常感谢。我现在几乎可以用了。我唯一搞不清楚的是“direct”字段从何而来。使用fiddler,我可以手动登录,将值复制并粘贴到我的代码中,生成cookie,然后直接登录到我想要的页面。唯一的问题是,不同用户的直接价值不同。页面源代码为。请你知道它是从哪里得到这个值的,如果有一种方法我可以自动得到它;这可能是为了阻止人们刮它。试着把它放进去,看看你是否能理解它。因此,您可以手动组合原始javascript。