博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
https redirects java_java HttpURLConnection 得到 Redirect 转向的例子
阅读量:6502 次
发布时间:2019-06-24

本文共 2528 字,大约阅读时间需要 8 分钟。

做网站的人,都知道可以在apache,iis里配置 302,302转向,这样对搜索引擎也是有好的。如果用java开发程序得到这个链接的话,通常是得不到真实的转向地址的,这需要手工处理。URL obj = new URL(url);

HttpURLConnection conn = (HttpURLConnection) obj.openConnection();

conn.setInstanceFollowRedirects(true); //you still need to handle redirect manully.

HttpURLConnection.setFollowRedirects(true);

如果在服务端,从原始的url 转向另一个url 那么得到的response 应该是:

301:moved permanently

302:temporary redirect

其实,可以通过 HTTP response 的 头部 :"Location" 得到这个转向的url。比如有一个例子,从http://www.twitter.com 自动转向https 的站点:https://www.twitter.com

一个例子如下:

import java.io.BufferedReader;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.URL;

public class HttpRedirectExample {

public static void main(String[] args) {

try {

String url = "http://www.twitter.com";

URL obj = new URL(url);

HttpURLConnection conn = (HttpURLConnection) obj.openConnection();

conn.setReadTimeout(5000);

conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");

conn.addRequestProperty("User-Agent", "Mozilla");

conn.addRequestProperty("Referer", "google.com");

System.out.println("Request URL ... " + url);

boolean redirect = false;

// normally, 3xx is redirect

int status = conn.getResponseCode();

if (status != HttpURLConnection.HTTP_OK) {

if (status == HttpURLConnection.HTTP_MOVED_TEMP

|| status == HttpURLConnection.HTTP_MOVED_PERM

|| status == HttpURLConnection.HTTP_SEE_OTHER)

redirect = true;

}

System.out.println("Response Code ... " + status);

if (redirect) {

// get redirect url from "location" header field

String newUrl = conn.getHeaderField("Location");

// get the cookie if need, for login

String cookies = conn.getHeaderField("Set-Cookie");

// open the new connnection again

conn = (HttpURLConnection) new URL(newUrl).openConnection();

conn.setRequestProperty("Cookie", cookies);

conn.addRequestProperty("Accept-Language", "en-US,en;q=0.8");

conn.addRequestProperty("User-Agent", "Mozilla");

conn.addRequestProperty("Referer", "google.com");

System.out.println("Redirect to URL : " + newUrl);

}

BufferedReader in = new BufferedReader(

new InputStreamReader(conn.getInputStream()));

String inputLine;

StringBuffer html = new StringBuffer();

while ((inputLine = in.readLine()) != null) {

html.append(inputLine);

}

in.close();

System.out.println("URL Content... \n" + html.toString());

System.out.println("Done");

} catch (Exception e) {

e.printStackTrace();

}

}

}

输出的结果为:

Request URL ... http://www.twitter.com

Response Code ... 301

Redirect to URL : https://twitter.com/

URL Content...

评论 ( What Do You Think )

名称

邮箱

网址

评论

验证

b2fc17868d24c303933fc7015e887109.png

转载地址:http://kqlyo.baihongyu.com/

你可能感兴趣的文章
因为文件组 'PRIMARY' 已满 解决办法
查看>>
Flume 读取实时更新的日志文件
查看>>
HDU 2049
查看>>
《Spring1之第十次站立会议》
查看>>
Unity Shader 噪声消融特效 - 剑灵死亡特效
查看>>
Eclipse 自动生成 Ant的Build.xml 配置文件
查看>>
添加一条信息到列表,如果重复就替换,
查看>>
C#基础第五天
查看>>
MEF 编程指南(六):导出和元数据
查看>>
宝明34
查看>>
python 小数相加报错 invalid literal for int() with base 10
查看>>
【ubuntu】linux链接库
查看>>
uva 12325 枚举暴力 b
查看>>
多线程问题(JVM重排序)
查看>>
LeetCode 459 Repeated Substring Pattern
查看>>
POJ 3268 Silver Cow Party
查看>>
进程线程及堆栈关系的总结
查看>>
Android Camera开发:使用TextureView和SurfaceTexture预览Camera 基础拍照demo
查看>>
EMLS项目推进思考
查看>>
Eclipse快捷键 10个最有用的快捷键
查看>>