Java 獲取網(wǎng)絡(luò)302重定向URL的方法
更新時(shí)間:2019年08月29日 16:54:04 作者:Boblim
在本篇文章里小編給大家整理的是關(guān)于Java 獲取網(wǎng)絡(luò)302重定向URL的方法以及相關(guān)知識(shí)點(diǎn),有興趣的朋友們參考下。
方法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)頁的絕對(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中遍歷集合的并發(fā)修改異常解決方案實(shí)例代碼
當(dāng)你遍歷集合的同時(shí),又往集合中添加或者刪除元素,就可能報(bào)并發(fā)修改異常,下面這篇文章主要給大家介紹了關(guān)于Java中遍歷集合的并發(fā)修改異常解決方案的相關(guān)資料,需要的朋友可以參考下2022-12-12解決Springboot啟動(dòng)報(bào)錯(cuò):類文件具有錯(cuò)誤的版本61.0,應(yīng)為?52.0
這篇文章主要給大家介紹了關(guān)于解決Springboot啟動(dòng)報(bào)錯(cuò):類文件具有錯(cuò)誤的版本?61.0,應(yīng)為?52.0的相關(guān)資料,這是查閱了網(wǎng)上的很多資料才解決的,分享給大家,需要的朋友可以參考下2023-01-01使用MyBatis攔截器實(shí)現(xiàn)sql查詢權(quán)限動(dòng)態(tài)修改代碼實(shí)例
這篇文章主要介紹了使用MyBatis攔截器實(shí)現(xiàn)sql查詢權(quán)限動(dòng)態(tài)修改代碼實(shí)例,為了不耦合,現(xiàn)在的方案是在需要鑒權(quán)的Mybatis?Mapper方法上增加一個(gè)注解,在運(yùn)行過程中判斷該注解存在即對(duì)sql進(jìn)行修改,需要的朋友可以參考下2023-08-08java事件處理模型知識(shí)點(diǎn)總結(jié)
在本篇文章里小辮給大家分享的是一篇關(guān)于java事件處理模型知識(shí)點(diǎn)總結(jié)內(nèi)容,有興趣的朋友們可以學(xué)習(xí)下。2021-01-01java開發(fā)ServiceLoader實(shí)現(xiàn)機(jī)制及SPI應(yīng)用
這篇文章主要為大家介紹了java開發(fā)ServiceLoader實(shí)現(xiàn)機(jī)制及SPI應(yīng)用,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10Spring Boot 2和Redis例子實(shí)現(xiàn)過程解析
這篇文章主要介紹了Spring Boot2發(fā)布與調(diào)用REST服務(wù)過程解析,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-11-11