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の出力である: - HttpURLConnectionクラスで

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

setFollowRedirectsと呼ばれるstatic方法がありますここにはjavadocの説明があります:

HTTPリダイレクトde 3xx)は にこのクラスが自動的に続かなければなりません。デフォルトではTrueです。アプレット はこの変数を変更できません。セキュリティマネージャが存在する場合、この メソッドは、セキュリティマネージャのcheckSetFactoryメソッドをまず に呼び出して、操作が許可されていることを確認します。これにより、 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 

はまた、302と301ではないが返されますので、あなたは、比較のためHTTP_MOVED_TEMP定数を使用する必要があります。

+0

申し訳ありませんが、私はそれを理解していないかもしれませんが、正確にはリダイレクトされたURLで200応答を取得していますか? –

+0

デフォルトでは、 'setFollowRedirects'はtrueです。つまり、自動的に新しいURLにリダイレクトされ、それから受け取ったレスポンス(200)を返します。 –

+0

これをfalseに設定すると、URLリダイレクトをチェックしないように設定しました。これは後で自分自身をループの中で実行します。 –

関連する問題