Http URL Connection无法处理重定向

时间:2017-03-08 08:06:18

标签: java android network-programming httpurlconnection androidhttpclient

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

我已尝试使用此代码处理网址,但在查看日志时,我发现它无法获取重定向的网址。在示例代码中,您将看到要获取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

1 个答案:

答案 0 :(得分:6)

HttpURLConnection课程中,有一个名为static的{​​{1}}方法,这是javadoc所说的内容:

  

设置HTTP重定向(响应代码为3xx的请求)是否应该   这个课程会自动跟着。默认为True。小程序   无法改变这个变量。如果有安全管理员,这个   方法首先调用安全管理器的checkSetFactory方法   确保允许操作。这可能会导致   SecurityException异常。

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

setFollowRedirects

输出:

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而不是301.因此,您需要使用302 https://images.unsplash.com/photo-1488869154849-3547ed9ed8dd?ixlib=rb-0.3.5&q=100&fm=jpg&crop=entropy&cs=tinysrgb&s=b688408cbd18238a8fd1b6355e8d563d 常量进行比较。