java 使用java登录网站

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7622011/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 20:43:57  来源:igfitidea点击:

Login on website with java

javahttppostlogin

提问by Borut Flis

I would like to login to a website with java. I use the org.apache.http and I already written

我想用java登录一个网站。我使用 org.apache.http 并且我已经写了

HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("https://accounts.google.com/ServiceLogin?      service=mail&passive=true&rm=false&continue=https%3A%2F%2Fmail.google.com%2Fmail%2F%3Fhl%3Dsl%26tab%3Dwm%26ui%3Dhtml%26zy%3Dl&bsv=llya694le36z&scc=1&ltmpl=default&");
try {
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("vb_login_username",
                    "XXX"));
nameValuePairs.add(new BasicNameValuePair("vb_login_password",
                    "XXX"));
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = client.execute(post);
BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
String line = "";
while ((line = rd.readLine()) != null) {
System.out.println(line);
            }
} catch (IOException e) {
e.printStackTrace();
        }

It sends the post form correctly i have tested, though i still cant login. The website I want to login is http://www.xtratime.org/forum/Any ideas for this or is there a different way?

它正确发送了我已经测试过的帖子表单,但我仍然无法登录。我想登录的网站是http://www.xtratime.org/forum/对此有什么想法还是有不同的方法?

采纳答案by palacsint

<form action="login.php?do=login" method="post" 
    onsubmit="md5hash(vb_login_password, vb_login_md5password, 
        vb_login_md5password_utf, 0)">
  • Before submit the page encode the password (onsubmit). You should do the same in the code.

  • The value of the actionattribute does not match with your code (https://accounts.google.com...). You should send the post request to the login.php?do=login.

  • 在提交页面之前对密码 ( onsubmit)进行编码。你应该在代码中做同样的事情。

  • action属性的值与您的代码 ( https://accounts.google.com...)不匹配。您应该将 post 请求发送到login.php?do=login.

And there are a lot of hidden fields:

而且还有很多隐藏字段:

<input type="hidden" name="s" 
    value="b804473163cb55dce0d43f9f7c41197a" />

<input type="hidden" name="securitytoken" 
    value="0dcd78b4c1a376232b62126e7ad568e0d1213f95" />

<input type="hidden" name="do" value="login" />     

<input type="hidden" name="vb_login_md5password" />

<input type="hidden" name="vb_login_md5password_utf" />

You should send these parameters too.

您也应该发送这些参数。

Usually it's easier to install an HttpFox Firefox Add-onan inspect the request for the post parameters than decoding the javascript.

通常,安装HttpFox Firefox 附加组件并检查对 post 参数的请求比解码 javascript更容易。

My browser sends these post parameters (captured with HttpFox, the password is pass1):

我的浏览器发送这些post参数(用HttpFox捕获,密码是pass1):

vb_login_username: user1
vb_login_password:
s: 5d8bd41a83318e683de9c55a38534407
securitytoken: 0dcd78b4c1a376232b62126e7ad568e0d1213f95
do: login
vb_login_md5password: a722c63db8ec8625af6cf71cb8c2d939
vb_login_md5password_utf: a722c63db8ec8625af6cf71cb8c2d939

Edit:

编辑

The following works for me, I can get the "Thank you for logging in"message in the printed html code:

以下对我有用,我可以在打印的 html 代码中收到“感谢您登录”消息:

final HttpClient client = new DefaultHttpClient();
final HttpPost post = new HttpPost(
        "http://www.xtratime.org/forum/login.php?do=login");
try {
    final List<NameValuePair> nameValuePairs = 
        new ArrayList<NameValuePair>(1);
    nameValuePairs.add(new BasicNameValuePair("vb_login_username",
            "my user"));
    nameValuePairs.add(new BasicNameValuePair("vb_login_password", ""));
    nameValuePairs.add(new BasicNameValuePair("s", ""));
    nameValuePairs.add(new BasicNameValuePair("securitytoken",
            "inspected with httpfox, like f48d01..."));
    nameValuePairs.add(new BasicNameValuePair("do", "login"));
    nameValuePairs.add(new BasicNameValuePair("vb_login_md5password",
            "inspected with httpfox, like 8e6ae1..."));
    nameValuePairs.add(new BasicNameValuePair(
            "vb_login_md5password_utf",
            "inspected with httpfox, like 8e6ae1..."));

    post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    final HttpResponse response = client.execute(post);
    final BufferedReader rd = new BufferedReader(new InputStreamReader(
            response.getEntity().getContent()));
    String line = "";
    while ((line = rd.readLine()) != null) {
        System.out.println(line);
    }
} catch (final IOException e) {
    e.printStackTrace();
}

回答by Eng.Fouad

I suggest you to use htmlunit:

我建议你使用htmlunit

HtmlUnit is a "GUI-Less browser for Java programs". It models HTML documents and provides an API that allows you to invoke pages, fill out forms, click links, etc... just like you do in your "normal" browser.

It has fairly good JavaScript support (which is constantly improving) and is able to work even with quite complex AJAX libraries, simulating either Firefox or Internet Explorer depending on the configuration you want to use.

It is typically used for testing purposes or to retrieve information from web sites.

HtmlUnit 是一个“用于 Java 程序的无 GUI 浏览器”。它为 HTML 文档建模并提供一个 API,允许您调用页面、填写表单、单击链接等……就像您在“普通”浏览器中所做的一样。

它具有相当好的 JavaScript 支持(不断改进),甚至可以使用非常复杂的 AJAX 库,根据您要使用的配置模拟 Firefox 或 Internet Explorer。

它通常用于测试目的或从网站检索信息。