Java 獲取網(wǎng)絡(luò)302重定向URL的方法
方法1:
import java.net.HttpURLConnection;
import java.net.URL;
import org.junit.Assert;
import org.junit.Test;
public class GetRedirectUrlTest {
@Test
public void test_getRedirectUrl() throws Exception {
String url="http://www.baidu.com/link?url=ByBJLpHsj5nXx6DESXbmMjIrU5W4Eh0yg5wCQpe3kCQMlJK_RJBmdEYGm0DDTCoTDGaz7rH80gxjvtvoqJuYxK";
String expectUrl="http://www.zhihu.com/question/20583607/answer/16597802";
String redictURL = getRedirectUrl(url);
Assert.assertEquals(expectUrl, redictURL);
}
/**
* 獲取重定向地址
* @param path
* @return
* @throws Exception
*/
private String getRedirectUrl(String path) throws Exception {
HttpURLConnection conn = (HttpURLConnection) new URL(path)
.openConnection();
conn.setInstanceFollowRedirects(false);
conn.setConnectTimeout(5000);
return conn.getHeaderField("Location");
}
}
方法2:
/**
* 處理跳轉(zhuǎn)鏈接,獲取重定向地址
* @param url 源地址
* @return 目標(biāo)網(wǎng)頁(yè)的絕對(duì)地址
*/
public String getAbsUrl(String url){
CloseableHttpClient httpclient = HttpClients.createDefault();
HttpClientContext context = HttpClientContext.create();
HttpGet httpget = new HttpGet(url);
CloseableHttpResponse response = null;
String absUrl = null;
try {
response = httpclient.execute(httpget, context);
HttpHost target = context.getTargetHost();
List<URI> redirectLocations = context.getRedirectLocations();
URI location = URIUtils.resolve(httpget.getURI(), target, redirectLocations);
System.out.println("Final HTTP location: " + location.toASCIIString());
absUrl = location.toASCIIString();
}catch(IOException e){
e.printStackTrace();
}catch (URISyntaxException e) {
e.printStackTrace();
}finally {
try {
httpclient.close();
response.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return absUrl;
}
以上就是2中最常用的方法,感謝大家對(duì)腳本之家的支持。
相關(guān)文章
java web開發(fā)之實(shí)現(xiàn)購(gòu)物車功能
這篇文章主要為大家詳細(xì)介紹了java web開發(fā)之實(shí)現(xiàn)購(gòu)物車功能的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
使用JPA雙向多對(duì)多關(guān)聯(lián)關(guān)系@ManyToMany
這篇文章主要介紹了使用JPA雙向多對(duì)多關(guān)聯(lián)關(guān)系@ManyToMany,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-06-06
Spring Security 多過(guò)濾鏈的使用詳解
本文主要介紹了Spring Security 多過(guò)濾鏈的使用,文中通過(guò)示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-07-07
如何在Springboot實(shí)現(xiàn)攔截器功能
其實(shí)spring boot攔截器的配置方式和springMVC差不多,只有一些小的改變需要注意下就ok了,下面這篇文章主要給大家介紹了關(guān)于如何在Springboot實(shí)現(xiàn)攔截器功能的相關(guān)資料,需要的朋友可以參考下2022-06-06
解決java.util.HashMap$Values?cannot?be?cast?to?java.ut的問(wèn)題
這篇文章主要介紹了解決java.util.HashMap$Values?cannot?be?cast?to?java.ut的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Java實(shí)現(xiàn)樹形菜單的方法總結(jié)
當(dāng)我們想要展示層級(jí)結(jié)構(gòu),如文件目錄、組織結(jié)構(gòu)或分類目錄時(shí),樹形菜單是一個(gè)直觀且有效的解決方案,本文為大家整理了java中幾種常見方法,希望對(duì)大家有所幫助2023-08-08
Java數(shù)據(jù)結(jié)構(gòu)之插入排序與希爾排序
在本篇文章,我們將為小伙伴們進(jìn)行排序概念的基本講解并具體講解其中的兩種基礎(chǔ)排序:插入排序和希爾排序,希望小伙伴們能夠從中有所收獲2023-04-04
淺談java switch如果case后面沒(méi)有break,會(huì)出現(xiàn)什么情況?
這篇文章主要介紹了淺談java switch如果case后面沒(méi)有break,會(huì)出現(xiàn)什么情況?具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨想小編過(guò)來(lái)看看吧2020-09-09

