Java 获取网络302重定向URL的方法
作者:Boblim 时间:2022-03-25 15:19:46
方法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:
/**
* 处理跳转链接,获取重定向地址
* @param url 源地址
* @return 目标网页的绝对地址
*/
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;
}
来源:https://www.cnblogs.com/fnlingnzb-learner/p/11429834.html
标签:Java,302,重定向
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java实现扑克牌程序
2023-11-11 12:09:52
![](https://img.aspxhome.com/file/2023/0/59340_0s.jpg)
SpringBoot2.0集成WebSocket实现后台向前端推送信息
2023-08-22 18:50:39
![](https://img.aspxhome.com/file/2023/8/58308_0s.png)
Java的动态分派和静态分派的实现
2023-10-09 12:58:37
一文带你了解SpringBoot的启动原理
2023-11-28 20:44:42
Android自定义TextView跑马灯效果
2023-08-07 01:14:57
Java泛型的简单实例
2023-11-27 01:03:38
flutter实现发送验证码功能
2023-07-05 19:03:12
![](https://img.aspxhome.com/file/2023/3/90173_0s.jpg)
基于Mock测试Spring MVC接口过程解析
2023-11-27 12:04:30
Android利用传感器实现微信摇一摇功能
2023-07-12 05:05:18
详解Java设计模式——命令模式
2023-11-23 05:00:07
![](https://img.aspxhome.com/file/2023/8/59688_0s.png)
Java继承Thread类创建线程类示例
2023-08-04 20:27:08
Java经典面试题最全汇总208道(五)
2023-11-10 07:06:46
Flutter Widgets MediaQuery控件屏幕信息适配
2023-06-29 04:48:21
![](https://img.aspxhome.com/file/2023/2/120092_0s.jpg)
java实现单链表、双向链表
2023-02-09 03:15:59
java中获取json的所有key方法
2023-10-15 06:15:26
Java使用pulsar-flink-connector读取pulsar catalog元数据代码剖析
2023-11-05 17:25:41
java 读取本地文件实例详解
2023-08-12 20:41:32
详解java 三种调用机制(同步、回调、异步)
2023-11-25 07:59:57
Spring Data Jpa 复杂查询方式总结(多表关联及自定义分页)
2023-11-21 10:01:27
![](https://img.aspxhome.com/file/2023/2/59722_0s.png)
List调用toString()方法后,去除两头的中括号实例
2023-09-28 11:18:56