2

我試圖用HttpURLConnection類的幫助打開連接。http URL連接無法處理重定向

我試過這段代碼來處理一個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 

回答

4

HttpURLConnection類,有一種方法static稱爲setFollowRedirects,這是它的javadoc說:

設置是否HTTP重定向(帶有響應co的請求de 3xx)應該自動地跟隨這個類別 。默認情況下爲真。小程序 不能更改此變量。如果有安全管理器,這個 方法首先調用安全管理器的checkSetFactory方法到 ,確保操作是允許的。這可能導致 SecurityException。

默認情況下,它始終是true,因此,您將獲得200個重定向URL響應。如果您不希望發生這種情況,則需要將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 

此外,它會返回302和301不那麼你需要使用HTTP_MOVED_TEMP常數比較。

+0

對不起,我可能還沒有明白,但究竟是獲取200響應與重定向的URL? –

+0

默認情況下,'setFollowRedirects'爲true,這意味着它會自動重定向到新的URL並返回從它接收到的響應(200)。 –

+0

因此將其設置爲false,我將其設置爲不檢查URL重定向,我將在後面在循環中做我自己,對吧? –