连接用户 jsoup 重定向错误
我正在尝试从我的网络服务器上的动态 php 页面获取 html 表。 Web 服务器是 Zabbix 前端。
这是我的示例代码:
doc = Jsoup.connect("http://192.168.209.178/zabbix/index.php").data("name", "Admin", "password", "password", "enter", "Enter", "request", "/zabbix/events.php").userAgent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.46 Safari/535.11").followRedirects(true).post();
System.out.println(doc.title());
System.out.println(doc.toString());
我将参数与数据放在一起,因此名称是登录名,密码显然是密码,请求是网址重定向。
当我尝试 tjis 代码时,本机会与“访客”Zabbix 用户建立连接。这意味着它无权查看我想要的内容。 jsoup 没有使用我的管理员登录。 当我尝试禁用 Zabbix 来宾用户时,出现错误:
IOException: Too many redirects occurred trying to load URL
我认为 jsoup 正在重定向后发布帖子。但重定向是在我的index.php 中进行的,所以...
我确实需要以管理员用户身份进行连接。解决方法是为来宾设置管理密码(我尝试过并且有效)。
感谢您的帮助, 问候。 奥利维尔.
I'm trying to get an html table from a dynamic php page on my web server. The web server is a Zabbix frontend.
Here is my sample code:
doc = Jsoup.connect("http://192.168.209.178/zabbix/index.php").data("name", "Admin", "password", "password", "enter", "Enter", "request", "/zabbix/events.php").userAgent("Mozilla/5.0 (Windows NT 6.1) AppleWebKit/535.11 (KHTML, like Gecko) Chrome/17.0.963.46 Safari/535.11").followRedirects(true).post();
System.out.println(doc.title());
System.out.println(doc.toString());
I put my params with data, so name is the login, password is obviously password, and request is an url redirect.
When I try tjis code, natively, connection is made whis "guest" Zabbix user. That means which have no rights to view what I want. jsoup is not using my Admin loggin.
When I try to disable guest user is Zabbix, I have an error:
IOException: Too many redirects occurred trying to load URL
I think jsoup is making the post after the redirection. But redirection is made in my index.php, so...
I realy need to connect as Admin user. A workarround would be to set administrative password to guest (I tryied and it works).
Thank for your help,
Regards.
Olivier.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
登录的用户通常存储在会话中。会话通常由 cookie 维护。然而,Jsoup 不会在后续/重定向的请求中保留 cookie。事实上,Jsoup 根本不会自动维护 cookie。基本上,您在重定向的页面中不再登录。在设计不良的网站中,假定 cookie 不可禁用,这确实可能会导致无限重定向循环。
您基本上有 2 个选择:
重写您的 Jsoup 代码,以便您手动维护 cookie。您可以通过
Response#cookies( )
。您可以通过Request#cookie()
。使用真正的 HTTP 客户端而不是 Jsoup 来维护 cookie。大多数 HTTP 客户端,甚至是 Java SE 内置的
URLConnection
,都允许您在后续 HTTP 请求中透明地维护 cookie,而无需经历手动收集和设置 cookie 的麻烦。最后,您将 HTTP 客户端的响应作为InputStream
获取,并将其提供给 Jsoup,Jsoup 还有一个采用InputStream
的parse()
方法。A logged-in user is often stored in the session. A session is usually maintained by cookies. Jsoup however doesn't retain cookies in subsequent/redirected requests. In fact, Jsoup does not automatically maintain cookies at all. Basically, you are not logged in anymore in the redirected page. In poorly designed websites which assume that cookies are undisableable this may indeed result in an infinite redirect loop.
You have basically 2 options:
Rewrite your Jsoup code so that you manually maintain the cookies. You can obtain response cookies by
Response#cookies()
. You can set cookies on the subsequent request byRequest#cookie()
.Use a real HTTP client instead of Jsoup to maintain the cookies. Most HTTP clients, even the Java SE builtin
URLConnection
, allow you to transparently maintain cookies in subsequent HTTP requests without going through the hassle of manually collecting and setting cookies. Finally, you get the response of the HTTP client asInputStream
and feed it to Jsoup which has also aparse()
method taking anInputStream
.