我知道这类问题有很多问题,但我没有找到这类问题的答案。我正在尝试登录网站,但我有两个问题。我将在下面发布源代码,之后我会解释出现了什么问题。
Connection.Response loginForm = Jsoup.connect("https://xxx.xx.x.xx/website_before_login/index.php")
.method(Connection.Method.GET)
.execute();
//System.out.println(loginForm);
Document document = Jsoup.connect("https://xxx.xx.x.xx/website_after_login/graph_view.php")
.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36")
.data("cookieexists", "false")
.data("login_username", "login")
.data("login_password", "password")
.data("action", "login")
.cookies(loginForm.cookies())
.post();
System.out.println(document);
在这种情况下,我得到以下错误:
> Exception in thread "main" org.jsoup.HttpStatusException: HTTP error fetching URL. Status=403, URL=https://xxx.xx.x.xx/website_after_login/view.php
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:682)
at org.jsoup.helper.HttpConnection$Response.execute(HttpConnection.java:629)
at org.jsoup.helper.HttpConnection.execute(HttpConnection.java:261)
at org.jsoup.helper.HttpConnection.post(HttpConnection.java:256)
at test.Test_1.main(Test_1.java:89)
我希望您知道此网站仅在连接到VPN后才能运行,因此此处不需要代理设置。另外,我在Java HTTPS Connections中关闭了证书验证,因为这也是我的问题之一。 知道如何解决问题吗?
我也尝试使用get()方法连接;而不是post(); - 结果如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Login to Website</title>
<meta http-equiv="Content-Type" content="text/html;charset=utf-8">
<style type="text/css">
<!--
BODY, TABLE, TR, TD {font-family: Verdana, Arial, Helvetica, sans-
serif; font-size: 12px;}
A {text-decoration: none;}
A:active { text-decoration: none;}
A:hover {text-decoration: underline; color: #333333;}
A:visited {color: Blue;}
-->
</style>
<script type="text/javascript">if (top != self) {top.location.href =
self.location.href;}</script>
<script type="text/javascript">var csrfMagicToken =
"sid:2cab4af55d51696dff403441da56e8f7777b205d,1501588419";var csrfMagicName
=
"__csrf_magic";</script>
<script src="/website/include/csrf/csrf-magic.js" type="text/javascript">
</script>
</head>
<body onload="document.login.login_username.focus()">
<form name="login" method="post" action="graph_view.php">
<input type="hidden" name="__csrf_magic"
value="sid:2cab4af55d51696dff403441da56e8f7777b205d,1501588419">
<input type="hidden" name="action" value="login">
<table id="login" align="center">
<tbody>
<tr>
<td colspan="2">
<center>
<img src="/website/images/auth_login.gif" border="0" alt="">
</center></td>
</tr>
<tr style="height:10px;">
<td></td>
</tr>
<tr>
<td id="error" colspan="2"><font color="#FF0000"><strong>Invalid User
Name/Password Please Retype</strong></font></td>
</tr>
<tr style="height:10px;">
<td></td>
</tr>
<tr id="login_row">
<td colspan="2">Please enter your Website user name and password
below:</td>
</tr>
<tr style="height:10px;">
<td></td>
</tr>
<tr id="user_row">
<td>User Name:</td>
<td><input type="text" name="login_username" size="40" style="width:
295px;" value=""></td>
</tr>
<tr id="password_row">
<td>Password:</td>
<td><input type="password" name="login_password" size="40"
style="width: 295px;"></td>
</tr>
<tr style="height:10px;">
<td></td>
</tr>
<tr>
<td><input type="submit" value="Login"></td>
</tr>
</tbody>
</table>
</form>
<script type="text/javascript">CsrfMagic.end();</script>
</body>
</html>
在HTML代码中有信息“无效的用户名/密码请重新输入”,但我确信提供的凭据是正确的。我不知道问题出在哪里......
我需要这样才能获得只有在登录网站后才能使用的图像。
答案 0 :(得分:0)
我在你的HTML代码中看到,有一个隐藏的输入字段,你没有发送到服务器。
<input type="hidden" name="__csrf_magic"
value="sid:2cab4af55d51696dff403441da56e8f7777b205d,1501588419">
尝试使用JSoup从中获取值,并将其作为数据提供给代码的帖子。这意味着你的帖子看起来像这样:
Document document = Jsoup.connect("https://xxx.xx.x.xx/website_after_login/graph_view.php")
.userAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.81 Safari/537.36")
.data("cookieexists", "false")
.data("login_username", "login")
.data("login_password", "password")
.data("action", "login")
.data("__csrf_magic", "sid:2cab4af55d51696dff403441da56e8f7777b205d,1501588419")
.cookies(loginForm.cookies())
.post();
让我知道它是否有效!