java - Jsoup 未获取最终重定向 URL

标签 java

目前,我正在尝试使用Jsoup来获取此网址的最终重定向网址:https://playoverwatch.com/en-us/search?q=Lolzword-1298 。它应该重定向到此网址:https://playoverwatch.com/en-us/career/pc/us/Lolzword-1298 ,但下面的代码不会重定向 url。

String url = "https://playoverwatch.com/en-us/search?q=Lolzword-1298";
org.jsoup.Connection.Response response = Jsoup.connect(url).followRedirects(true).execute();
System.out.println(response.url());

有什么想法吗?

最佳答案

JSoup 的 followRedirects 用于 HTTP 重定向(“服务器重定向”),而您尝试使用的 URL 没有这样的东西。

包含您自己的问题的快捷方式 URL 的示例:

String url = "https://stackoverflow.com/q/44769507";
Connection.Response response = Jsoup.connect(url).followRedirects(true).execute();
System.out.println(response.url());
// https://stackoverflow.com/questions/44769507/jsoup-not-obtaining-final-redirected-url

由于 HTTP 重定向,这会打印不同的 URL,请参阅 header :

% curl -I 'https://stackoverflow.com/q/44769507'
HTTP/1.1 302 Found
Content-Type: text/html; charset=utf-8
Location: https://stackoverflow.com/questions/44769507/jsoup-not-obtaining-final-redirected-url
...

如果我尝试使用您的 URL,则不会出现 HTTP 重定向:

% curl -I 'https://playoverwatch.com/en-us/search?q=Lolzword-1298'
HTTP/1.1 200 OK
Cache-Control: public;max-age=300
Content-Length: 104248
Content-Type: text/html; charset=utf-8
...

当您加载页面时,您在浏览器中看到的“重定向”发生得更晚,使用 JavaScript(如果您在该页面上禁用 JavaScript,页面将保留在其原始位置),并且我认为 JSoup 无法捕获那个。

This answer指向 SeleniumHtmlUnit作为能够执行 JavaScript 并允许您获取最终 URL 的替代方案。

关于java - Jsoup 未获取最终重定向 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44769507/

相关文章:

c# - 如何从单个应用程序连接到多个 Oracle Coherence 缓存?

java - Excel/OLAP 和 HTTP 基本授权 - Excel 能否记住它已登录?

java - JSP/javabean/servlet MVC

java - 在 Ubuntu 上安装 Oracle 8 JDK

java - jsp页面被下载而不是被显示

java - 我应该如何修复 java 路径,以便运行 tomcat?

java - 使用 Nifi 预处理大文件

java - 使用 flavor 维度时设置 Android applicationId 的正确方法是什么?

java - 使用 jsoup 解析脚本(JSON 数据)

java - 将 Delphi 5 引擎控制应用程序转换为 Android - 需要有关方法的建议