How to detect a redirected URL?

2019-03-04 15:03发布

I'm working on an app that takes URLs from RSS feeds and opens them in a video player. It works if the URL is directly to the mp4/whatever file, but if I have a redirected URL (e.g. This one which redirects here) my player crashes and fails.

Is there any way to detect if its redirecting, and to which URL?

Thanks in advance :)

标签: android http
2条回答
劫难
2楼-- · 2019-03-04 15:55

for redirected url you may try this

try {
    HttpResponse response = client.execute(getRequest);
    final int statusCode = response.getStatusLine().getStatusCode();
    if(statusCode == HttpStatus.SC_MOVED_PERMANENTLY || statusCode == HttpStatus.SC_MOVED_TEMPORARILY){
        String location = response.getHeaders("Location")[0].toString();
        String redirecturl = location.replace("Location: ", "");
    }
}
查看更多
我只想做你的唯一
3楼-- · 2019-03-04 15:59

I made a test for you with my firefox10:
I typed your non-redirected url in my browser, and it will show the download page very soon, but for the redirected url, I aslo have to wait for a while to see the download page(more than 10s). So I guess accessing redirected url is much slower than the non-redirected one.

查看更多
登录 后发表回答