Abhiroj Panwar
Abhiroj Panwar

Reputation: 528

Http URL Connection unable to handle redirects

I'm trying to open a connection with the help of HttpURLConnection class.

I've tried this code to handle a URL, but on seeing Logs, I find that it's unable to get the redirected URLs. In the sample code, you will see that to get the URL, I'm using a while Loop, and due to this it's logging the same URL. I'm eagerly looking for a better fix for the problem. I hope the problem is clear.

Here is the sample code that I'm using :-

 Log.d(TAG,"URL received : --- >"+downloadURL);
            URL url=new URL(downloadURL);
            HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
            httpURLConnection.setDoInput(true);
            httpURLConnection.setInstanceFollowRedirects(true);
            httpURLConnection.connect();
            Log.d(TAG,httpURLConnection.getResponseMessage()+" Append with "+httpURLConnection.getResponseCode());
            while(httpURLConnection.getResponseCode()==HttpURLConnection.HTTP_MOVED_PERM)
            {
                httpURLConnection.getInputStream();
                URL url1=httpURLConnection.getURL();
            Log.d(TAG,"Redirected URL = "+url1);
            }

             InputStream inptStream=httpURLConnection.getInputStream();

Here is the output of logcat :-

03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.814 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.815 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.815 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.815 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download
03-08 13:28:38.815 22597-23668/abhirojpanwar.tablayout D/AsyncImageDownloader: Redirected URL = https://unsplash.com/photos/65sru5g6xHk/download

Upvotes: 3

Views: 6490

Answers (1)

Darshan Mehta
Darshan Mehta

Reputation: 30809

In HttpURLConnection class, there is a static method called setFollowRedirects, here's what it's javadoc says:

Sets whether HTTP redirects (requests with response code 3xx) should be automatically followed by this class. True by default. Applets cannot change this variable. If there is a security manager, this method first calls the security manager's checkSetFactory method to ensure the operation is allowed. This could result in a SecurityException.

By default it's always true and hence, you will get 200 response with redirected URL. If you don't want that to happen then you need to set setFollowRedirects to false. Below snippet demonstrates this:

URL url=new URL("https://unsplash.com/photos/65sru5g6xHk/download");
HttpURLConnection.setFollowRedirects(false);
HttpURLConnection httpURLConnection=(HttpURLConnection) url.openConnection();
httpURLConnection.setDoInput(true);
httpURLConnection.connect();
System.out.println(httpURLConnection.getResponseCode());
if(httpURLConnection.getResponseCode()==HttpURLConnection.HTTP_MOVED_TEMP){
   URL redirectUrl = new URL(httpURLConnection.getHeaderField("Location"));
   System.out.println(redirectUrl);
}
InputStream inptStream=httpURLConnection.getInputStream();

Output:

302
https://images.unsplash.com/photo-1488869154849-3547ed9ed8dd?ixlib=rb-0.3.5&q=100&fm=jpg&crop=entropy&cs=tinysrgb&s=b688408cbd18238a8fd1b6355e8d563d

Also, it returns 302 and not 301. So you need to use HTTP_MOVED_TEMP constant for comparison.

Upvotes: 9

Related Questions