URLConnection不遵循重定向 [英] URLConnection Doesn't Follow Redirect

查看:185
本文介绍了URLConnection不遵循重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法理解为什么Java的 HttpURLConnection 不遵循重定向。我使用以下代码获取此页面

I can't understand why Java's HttpURLConnection doesn't follow redirect. I use the following code to get this page:

import java.net.URL;
import java.net.HttpURLConnection;
import java.io.InputStream;

public class Tester {

    public static void main(String argv[]) throws Exception{
        InputStream is = null;

        try {
            String bitlyUrl = "http://bit.ly/4hW294";
            URL resourceUrl = new URL(bitlyUrl);
            HttpURLConnection conn = (HttpURLConnection)resourceUrl.openConnection();
            conn.setConnectTimeout(15000);
            conn.setReadTimeout(15000);
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows; U; Windows NT 6.0; ru; rv:1.9.0.11) Gecko/2009060215 Firefox/3.0.11 (.NET CLR 3.5.30729)");
            conn.connect();
            is = conn.getInputStream();
            String res = conn.getURL().toString();
            if (res.toLowerCase().contains("bit.ly"))
                System.out.println("bit.ly is after resolving: "+res);
       }
       catch (Exception e) {
           System.out.println("error happened: "+e.toString());
       }
       finally {
            if (is != null) is.close(); 
        }
    }
}

此外,我得到以下信息响应(看起来绝对正确!):

Moreover, I get the following response (it seems absolutely right!):

GET /4hW294 HTTP/1.1
Host: bit.ly
Connection: Keep-Alive
User-Agent: Mozilla/5.0 (Windows; U; Windows NT 6.0; ru-RU; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729)
HTTP/1.1 301 Moved
Server: nginx/0.7.42
Date: Thu, 10 Dec 2009 20:28:44 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Location: https://www.myganocafe.com/CafeMacy
MIME-Version: 1.0
Content-Length: 297

不幸的是, res 变量包含相同的URL和stream包含以下内容(显然,Java的 HttpURLConnection 不遵循重定向!):

Unfortunately, the res variable contains the same URL and stream contains the following (obviously, Java's HttpURLConnection doesn't follow redirect!):

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<HTML>
<HEAD>
<TITLE>Moved</TITLE>
</HEAD>
<BODY>
<H2>Moved</H2>
<A HREF="https://www.myganocafe.com/CafeMacy">The requested URL has moved here.</A>
<P ALIGN=RIGHT><SMALL><I>AOLserver/4.5.1 on http://127.0.0.1:7400</I></SMALL></P>
</BODY>
</HTML>


推荐答案

我认为它不会自动重定向HTTP到HTTPS(反之亦然)。

I don't think that it will automatically redirect from HTTP to HTTPS (or vice-versa).

尽管我们知道它反映了HTTP,但从HTTP协议的角度来看,HTTPS只是其他一些,完全不同,未知协议。在没有用户批准的情况下遵循重定向是不安全的。

Even though we know it mirrors HTTP, from the HTTP protocol point of view, HTTPS is just some other, completely different, unknown protocol. It would be unsafe to follow the redirect without user approval.

例如,假设应用程序设置为自动执行客户端身份验证。用户希望匿名冲浪,因为他正在使用HTTP。但是,如果他的客户端在没有询问的情况下跟踪HTTPS,他的身份就会泄露给服务器。

For example, suppose the application is set up to perform client authentication automatically. The user expects to be surfing anonymously because he's using HTTP. But if his client follows HTTPS without asking, his identity is revealed to the server.

这篇关于URLConnection不遵循重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆