java - Http URL 连接无法处理重定向

标签 java android network-programming httpurlconnection androidhttpclient

我试图在 HttpURLConnection 类的帮助下打开一个连接。

我试过这段代码来处理一个 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 类中,有一个名为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。下面的 fragment 演示了这一点:

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

此外,它返回 302 而不是 301。因此您需要使用 HTTP_MOVED_TEMP 常量进行比较。

关于java - Http URL 连接无法处理重定向,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42666024/

相关文章:

android studio 无法激活 AppCompatActivity

java - 使用 onClick 监听器事件显示图像

android - 如何在 Eclipse 中使用来自 PhoneGap Build 和 PhoneGap Android 项目的相同源代码

c++ - 单个tcp客户端服务器程序中是否允许多个select()

c - 如何编辑proc文件?

python : How do server come to know of network failure at the client side?

java - REGEX 从图像文件名中获取元素

java - jackson 漂亮的印花绳

java - NoClassDefFoundError 位于 edu/knowitall/openie/openIECli

java - 如何将以下引用变量转换为 Java JNA?