3

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
Micho
  • 3,929
  • 13
  • 37
  • 40
Abhiroj Panwar
  • 485
  • 9
  • 19

1 Answers1

9

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.

Darshan Mehta
  • 30,102
  • 11
  • 68
  • 102
  • Sorry, I may not have understood it but what exactly is getting 200 responses with redirected URL? – Abhiroj Panwar Mar 08 '17 at 08:33
  • By default, `setFollowRedirects` is true, that means it will automatically redirect to the new URL and return the response received from it (which is 200). – Darshan Mehta Mar 08 '17 at 08:36
  • so setting it to false, I set it to not check for URL redirects, which I will be later doing my self in the loop, right? – Abhiroj Panwar Mar 08 '17 at 08:40
  • Yes, exactly. That's what you need to do – Darshan Mehta Mar 08 '17 at 08:43
  • After applying the fix, infinite loop has stopped, but it is still showing the redirected URL same as URL it received before running the check. Any insights about where I might be going wrong? – Abhiroj Panwar Mar 08 '17 at 08:45
  • Redirect URL will be in `location` header. I have updated my answer to extract the redirect url from location header (in `if`), could you please have a look. – Darshan Mehta Mar 08 '17 at 08:56
  • It is still posting the same solution, I might be going wrong somewhere. can you please post the output of your screen? @DarshanMehta – Abhiroj Panwar Mar 08 '17 at 09:13
  • I have added the output. Have you tried with my updated code in the answer (the one that extracts location header)? – Darshan Mehta Mar 08 '17 at 09:23
  • Yeah, I was missing the setFollowRedirects(False). I might have overlooked it when I removed the code. Thanx :) – Abhiroj Panwar Mar 08 '17 at 10:24
  • I'm probably late to this. But for some reason in my Java application, this code does not work. If i open via a browser through the application it redirects, but if I do this, the redirect never happens. Been banging my head for a little bit. I'm implementing a docusign API and it requires an Authorization code first to use the API. You get an Auth code from a redirected url you get from making a request. – Nathan Donaldson May 07 '22 at 00:15