这是我尝试使用我的方法打开的链接:
这是我打开它的代码,但它不起作用......
public static Document loadURL(String arg) throws IOException
{
/*
* This is a method that fake an agent and make a document out of
* a url link.
*/
try
{
Document doc = Jsoup.connect(arg)
.data("query", "Java")
.userAgent("Mozilla")
.cookie("auth", "token")
.timeout(10000)
.post();
return doc;
}
我首先想知道 ?token= 这个词到底是什么意思?
为什么我的浏览器可以打开链接,而这个方法却打不开?
如果网络浏览器可以做到这一点,我如何使用 jsoup 来做同样的事情?
我搜索了一下,我有点明白这是一个cookie身份验证,但我不明白细节。(如果我知道细节,我可能会知道如何伪造它......)
如果您想知道页面上的链接在哪里,它就在这里: https://rainbow-highway.deviantart.com/art/Sugar-Rush-652802405
我是否需要此页面中的 cookie 来重定向,或者我只需要 ?token= ?????? 末尾的花哨的十六进制代码
链接在下载按钮中,这是我自己的DA页面。
最佳答案
我找到了一个解决方案,它适用于异常艺术网站; 灵感来自 github 中的一个项目。
Map<String,String> cookies = new HashMap<String , String>();
org.jsoup.Connection.Response resp =
Jsoup.connect("https://sorcerushorserus.deviantart.com/art/Dash-Academy-2-Hot-Flank-Part8-263131103")
.userAgent("Mozilla").execute();
cookies = resp.cookies();
Document doc = resp.parse();
Elements eles = doc.select("a.dev-page-download");
if(eles.size()>0)
{
System.out.println("We have the downlaod link on this page!");
String downloadlink = eles.get(0).attr("href");
System.out.println(":"+downloadlink);
HttpURLConnection con = (HttpURLConnection) new URL(downloadlink).openConnection();
String cookiesString = "";
for(Map.Entry<String, String> entry:cookies.entrySet())
{
cookiesString = cookiesString+entry.getKey()+"="+entry.getValue()+";";
}
cookiesString = cookiesString.substring(0, cookiesString.length()-1);
System.out.println("Cookie String: "+cookiesString);
con.setRequestProperty("Cookie", cookiesString);
con.setRequestProperty("User-Agent","Mozilla");
con.setInstanceFollowRedirects(true);
con.connect();
int respondcode = con.getResponseCode();
Thread.sleep(10000);
String location = con.getURL().toString();
System.out.println("response code: "+ respondcode);
System.out.println("image location: "+location);
con.disconnect();
所以......酷......
关于java - 使用 jsoup 获取链接并将其重定向到最后带有 ?token= 的链接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46375185/