Java 301 永久搬家

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18431440/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 01:34:07  来源:igfitidea点击:

301 Moved Permanently

javahtmlhttphttp-status-code-301

提问by Tony

I'm trying to get HTML by URL in Java. But 301 Moved Permanentlyis all that I've got. Another URLs work. What's wrong? This is my code:

我正在尝试通过 Java 中的 URL 获取 HTML。但这301 Moved Permanently就是我所拥有的。另一个网址有效。怎么了?这是我的代码:

 hh= new URL("http://hh.ru");
        in = new BufferedReader(
                new InputStreamReader(hh.openStream()));


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

            sb.append(inputLine).append("\n");
            str=sb.toString();//returns 301


        }

采纳答案by Jk1

You're facing a redirect to other URL. It's quite normal and web site may have many reasons to redirect you. Just follow the redirect based on "Location" HTTP header like that:

您正面临重定向到其他 URL。这是很正常的,网站可能有很多理由重定向您。只需遵循基于“位置”HTTP标头的重定向,如下所示:

URL hh= new URL("http://hh.ru");
URLConnection connection = hh.openConnection();
String redirect = connection.getHeaderField("Location");
if (redirect != null){
    connection = new URL(redirect).openConnection();
}
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
System.out.println();
while ((inputLine = in.readLine()) != null) {
    System.out.println(inputLine);
}

Your browser is following redirects automaticaly, but using URLConnection you should do it by your own. If it bothers you take a look at other Java HTTP clientimplementations, like Apache HTTP Client. Most of them are able to follow redirect automatically.

您的浏览器会自动跟踪重定向,但使用 URLConnection 您应该自己完成。如果它困扰您,请查看其他Java HTTP 客户端实现,例如 Apache HTTP 客户端。他们中的大多数都能够自动跟随重定向。

回答by ProgramFOX

There's nothing wrong with your code. The message means that hh.ruis permanently moved to another domain.

你的代码没有任何问题。该消息意味着hh.ru永久移动到另一个域。

回答by Soosh

I tested your code and it is ok, but when I use "hh.ru", the same problem as yours, and when I use lynx(command line browser) to connect to "hh.ru", it will show me that it is redirecting to another url and then show me that it is moved permanently and after that this alert:
"Alert!: This client does not contain support for HTTPS URLs"

我测试了你的代码,没问题,但是当我使用“hh.ru”时,和你的问题一样,当我使用 lynx(命令行浏览器)连接到“hh.ru”时,它会告诉我它正在重定向到另一个 url,然后向我显示它已永久移动,然后显示此警报:
“警报!:此客户端不包含对 HTTPS URL 的支持”

回答by Aviator

found this answer useful and improved a little due to the possibility of multiple redirections (e.g. 307 then 301).

由于多次重定向的可能性(例如 307 然后是 301),发现这个答案很有用并且有所改进。

URLConnection urlConnection = url.openConnection();
                String redirect = urlConnection.getHeaderField("Location");
                for (int i = 0; i < MAX_REDIRECTS ; i++) {
                    if (redirect != null) {
                        urlConnection = new URL(redirect).openConnection();
                        redirect = urlConnection.getHeaderField("Location");
                    } else {
                        break;
                    }
                }

回答by kimoduor

I resolved mine when I put the specific file running on the server. Instead of http://hh.ru, I used http://hh.ru/index.php. It worked for me

当我将特定文件在服务器上运行时,我解决了我的问题。相反http://hh.ru,我使用了http://hh.ru/index.php. 它对我有用

回答by Vishrant

Check if the URL provided is HTTP or HTTPS, consider adding protocol is you are using only domain name like http(s)://domainname.com/resource-name

检查提供的 URL 是 HTTP 还是 HTTPS,考虑添加协议是否您只使用域名,如 http(s)://domainname.com/resource-name

Read: https://en.wikipedia.org/wiki/HTTP_301

阅读:https: //en.wikipedia.org/wiki/HTTP_301