How to detect a redirected URL?

2019-03-04 15:10发布

问题:

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 :)

回答1:

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: ", "");
    }
}


回答2:

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.



标签: android http