简体   繁体   English

使用C#登录网页

[英]Login to web page using C#

I would like to login to a certain web page , and download few pages as logged in user. 我想登录到某个网页 ,并以登录用户的身份下载几个页面。 I think I theoretically know what to do, based on an answer to this stackoverflow question , but I have no idea, where in the address should I put the login credentials. 我认为从理论上来说,基于对这个stackoverflow问题的回答,我知道该怎么做,但是我不知道应该在哪里将登录凭据放在地址中。 In the question above, it is quite obvious, but it is not so obvious at Aukro. 在上面的问题中,这很明显,但是在Aukro中并不是那么明显。
I'd be very grateful for any avice... :) 我将非常感谢任何建议... :)

One tricky part is the fact that this login page sets a bunch of session cookies and appends some unique query string parameters for your session. 一个棘手的部分是,该登录页面设置了一堆会话cookie,并为会话添加了一些唯一的查询字符串参数。 Presumably, you'll need to retrieve good values from the server for each session you create. 大概,您需要为创建的每个会话从服务器检索良好的值。

When you land on https://ssl.aukro.cz/enter_login.php , you get a 302 Found (which in practice is used for redirects). 当您登陆https://ssl.aukro.cz/enter_login.php时 ,您将获得302 Found(实际上用于重定向)。 The full request looks like this: 完整的请求如下所示:

Date    Wed, 26 Aug 2009 17:50:05 GMT
Server  Apache
Vary    Accept-Encoding
Set-Cookie  ws2=acda7c76687f; expires=Wed, 26-Aug-2009 18:20:05 GMT; path=/; domain=.aukro.cz
Expires Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control   no-store, no-cache, must-revalidate, post-check=0, pre-check=0
Pragma  no-cache
Location    https://ssl.aukro.cz/enter_login.php?session=NmQ2YQFRBVABVgFRVFQAXVRXVFZXWlcHBVRVWAcGUF0OUVJWVVRRXFVUBFFTAAcGAFxUA1NSY2JkZQ%3D%3D&global_login_hash=e16bd60f566a0ae3752997bf21844c4ec2bd0d22&session_login_hash=fecd7825582b6d038d288f67c368090aa369c85d&url=OTFhY1hFRkYDHk4UR0YcV0xaEwweUkgZXEMTDEIeU1VaVBIQb1RARFZDTxNYQQ1YVm4FCkIMAzAxMjY%3D
Content-Encoding    gzip
Content-Length  26
Keep-Alive  timeout=2, max=9999
Connection  Keep-Alive
Content-Type    text/html; charset=UTF-8

You'll need to grab the URL location and the cookie it sets ("ws2"). 您需要获取URL位置及其设置的cookie(“ ws2”)。 Then you'll need to drop onto that redirect URL you grabbed, and get the rest of the cookies it sets, which are ws2 , stsd_refr , and qeppo_login . 然后,您需要放到您获取的重定向URL上,并获取它设置的其余cookie,分别是ws2stsd_refrqeppo_login

You then need to fill in the POST body with the form elements as mentioned above and in the links you provided. 然后,您需要使用上面提到的表单元素以及您提供的链接来填充POST正文。

Not knowing anything about this site you're trying to log into, I'd venture a guess that if you don't get good session variables and cookies and include them for each of your logical requests, your login attempts may fail. 不知道您要登录的这个站点的任何信息,我冒险猜测一下,如果您没有获得良好的会话变量和cookie并将其包含在每个逻辑请求中,则登录尝试可能会失败。

Long story short, just use Fiddler or Firebug to capture what a login session looks like, and mimic it using the techniques you've already seen. 长话短说,只需使用Fiddler或Firebug捕获登录会话的外观,然后使用您已经看到的技术对其进行模仿。

This might be a valid alternative technique 这可能是一种有效的替代技术

The needed controls for this are: 所需的控件为:

id="user_login" ID = “USER_LOGIN”

id="user_password" ID = “USER_PASSWORD”

value="Přihlásit" (apparently czech for "Login") value =“Přihlásit”(显然是“登录”的捷克语)

You will need to view the source of the webpage, and find the <form> that is used for login. 您将需要查看网页的来源,并找到用于登录的<form> That will give you the URL that you will need to post to. 这将为您提供您需要发布到的URL。 You will need to specify all the variables that are in <input> tags, just the way they did in the question you linked. 您将需要指定<input>标记中的所有变量,就像它们在链接的问题中所做的一样。

In this case you will need to specify: session, global_login_hash, session_login_hash, url, request_server, user_login, user_password 在这种情况下,您需要指定: session, global_login_hash, session_login_hash, url, request_server, user_login, user_password

One way to determine what needs to be submitted is to use a proxy like Fiddler2 , then use a web browser with its proxy set to Fiddler and log into the webpage. 确定需要提交的内容的一种方法是使用Fiddler2之类的代理,然后使用其代理设置为Fiddler的Web浏览器并登录到网页。 Fiddler will give you the request and response headers and text. Fiddler将为您提供请求和响应的标题和文本。

You need to create HTTPS POST request supplying the following values in an application/x-www-form-urlencoded body: 您需要创建HTTPS POST请求,并在application/x-www-form-urlencoded正文中提供以下值:

user_login = <user name>
user_password = <password>
session = "ZTEzMw9RAAcGUlIABQgDVlcBBVAAB1NRAAFQClAAUQADCAdRDgNRAQcDVwsHBQADVAYAAAYBNjA1Yg=="
global_login_hash = "c6da0c2fa41454f62c80d9cc688f4303ebebb9b3"
session_login_hash = "8e5190abcb4cccee78b7331a616c4fb723f7fe41"
url = "OTFhY1hFRkYDHk4UR0YcV0xaEwweUkgZXEMTDEIeU1VaVBIQb1RARFZDTxNYQQ1YVm4FCkIMAzAxMjY="
request_server = "ssl.aukro.cz"

You might start by suppling values for user_login and user_password and ignore the rest, but it is impossible to know exactly what the server requries. 您可能首先提供user_loginuser_password值,然后忽略其余值,但是不可能确切知道服务器需要什么。 The encoded values are certainly not reusable and if required you will have to scrape them from the login page before you login. 编码后的值当然不可重用,并且如果需要,则必须在登录之前从登录页面中将其擦除。

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

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