如何以编程方式登录网站 [英] How to Programmatically Log in to a Website
问题描述
我不知道如何以编程方式登录此网站 我搜索了stackoverflow,发现了此,但我仍然不知道不知道在URL或URI中放入什么.
I dont know how to programmatically login to this site I've searched through stackoverflow and found this, but I still don't know what to put into URL or URI.
推荐答案
当我只输入用户名'abc'和密码'def'并单击按钮时,我将获得以下发布数据:
When I just type in username 'abc' and password 'def' and hit the button I get the following post data:
next = apps%2Flinks%2F& why = pw& email = abc& password = def& fw_human =
next=apps%2Flinks%2F&why=pw&email=abc&password=def&fw_human=
因此,如果您仅使用该帖子数据并将其替换为适当的信息,就可以使我相信,您可以模拟手动登录.
So that leads me to beleive if you just use that post data and replace it with the appropriate information, you can simulate a manual login.
因此,从您链接的堆栈溢出中,将采用以下形式:
So from the stack overflow you linked, this would take the form:
string formUrl = "http://ratings-plus.webs.com/apps/auth/doLogin"; // NOTE: This is the URL the form POSTs to, not the URL of the form (you can find this in the "action" attribute of the HTML's form tag
string formParams = string.Format("next=apps%2Flinks%2F&why=pw&email={0}&password={1}&fw_human=", "your email", "your password");
string cookieHeader;
WebRequest req = WebRequest.Create(formUrl);
req.ContentType = "application/x-www-form-urlencoded";
req.Method = "POST";
byte[] bytes = Encoding.ASCII.GetBytes(formParams);
req.ContentLength = bytes.Length;
using (Stream os = req.GetRequestStream())
{
os.Write(bytes, 0, bytes.Length);
}
WebResponse resp = req.GetResponse();
cookieHeader = resp.Headers["Set-cookie"];
请注意,在将来的请求中,您将需要使用返回的任何cookie值来保持您的身份验证状态.
Note that in future requests you will need to then use whatever the cookie value that is returned to maintain your authenticated status.
这篇关于如何以编程方式登录网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!