如果永久移动,则获取新的Url [英] Getting new Url if Moved Permanently

查看:128
本文介绍了如果永久移动,则获取新的Url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为一个项目开发代码,其中部分代码是检查Urls(网站)的列表是否正常并确认。

I am developing a code for a project where a part of the code is to check a list of Urls (Web site) is live and and confirm it.

到目前为止,所有事情都按计划进行,期望一些页面永久移动,错误301有关此列表。如果出现错误301,我需要获取新的Url信息并在返回true之前将其传递给方法。

So far every thing is working as planned, expect some pages that are Moved Permanently with error 301 regarding this list. In case of error 301 I need to get the new Url info and pass it in a method before returning true.

以下示例仅移至https,但其他示例可以被移动到另一个网址,所以如果你打电话给这个网站:

The following example is just move to https but other examples could be moved to another Url, so if you call this site:

http://en.wikipedia.org/wiki/HTTP_301

它移至

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

这是很好,我只需要获得新的Url。

Which is fine, I just need to get the new Url.

这可能吗?怎么样?

这是我的到目前为止工作代码部分:

This is my working code part so far:

boolean isUrlOk(String urlInput) {
    HttpURLConnection connection = null;
    try {
        URL url = new URL(urlInput);
        connection = (HttpURLConnection) url.openConnection();
        connection.setRequestMethod("GET");
        connection.connect();
        urlStatusCode = connection.getResponseCode();

    } catch (IOException e) {
        // other error types to be reported
        e.printStackTrace();
    }
    if (urlStatusCode == 200) {
        return true;
    } else if (urlStatusCode == 301) {
        // call a method with the correct url name
        // before returning true
        return true;
    }
    return false;
}


推荐答案

您可以获取新的URL with

You can get the new URL with

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

这篇关于如果永久移动,则获取新的Url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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