Http URL 连接无法处理重定向

Http URL Connection unable to handle redirects

我正在尝试在 HttpURLConnection class 的帮助下打开连接。

我试过这段代码来处理 URL,但是在查看日志时,我发现它无法获得重定向的 URL。在示例代码中,您将看到为了获得 URL,我使用了 while 循环,因此它记录了相同的 URL。我急切地寻找更好的解决方案。我希望问题很清楚。

这是我正在使用的示例代码:-

 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();

这是 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

HttpURLConnection class 中,有一个名为 setFollowRedirectsstatic 方法,javadoc 是这样说的:

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.

默认情况下它始终是 true,因此,您将获得重定向 URL 的 200 响应。如果您不希望这种情况发生,则需要将 setFollowRedirects 设置为 false。下面的片段演示了这一点:

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();

输出:

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

另外,returns302不是301,所以需要用HTTP_MOVED_TEMP常量进行比较。