2017-09-23 36 views
1

這裏是我嘗試使用我的方法來打開鏈接:使用jsoup獲取和重定向鏈接的鏈接與令牌=到底

https://www.deviantart.com/download/652802405/sugar_rush_by_rainbow_highway-dasntk5.png?token=2080f7bdd7953b1b5128e45fe1f8d06c6ba8c4f1&ts=1506130109

這裏是我打開它的代碼和它不工作....

​​

我想首先知道這個詞是什麼?token =最後意味着什麼?

爲什麼我的瀏覽器可以打開鏈接,而這種方法不能?

如果Web瀏覽器可以做到這一點,我怎麼能用jsoup做同樣的事情?

我搜索,我有點明白了吧,這是一個餅乾身份驗證,但我不明白的細節。(我大概會知道如何捏造事實,如果我知道的細節...)

在你想知道哪裏是頁面上的鏈接時,它是在這裏: https://rainbow-highway.deviantart.com/art/Sugar-Rush-652802405

我需要的餅乾在此頁面重定向或我只需要花哨的十六進制代碼在的結束標記? = ?????

鏈接在下載中,它是我自己的DA頁面。

回答

0

我找到了一個解決方案,它適用於網站偏差藝術; 受github項目啓發。

https://github.com/4pr0n/ripme/blob/master/src/main/java/com/rarchives/ripme/ripper/rippers/DeviantartRipper.java

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(); 

左右....爽...