如何知道链接将被重定向

我遇到了这种类型的问题 – 当我尝试下载某些网站的favicon时,您会遇到重定向(例如google.com / favicon.ico – > www.google.com / favicon.ico)。 在这种情况下,我的代码不保存图像。 我想知道是否可以知道页面是否被重定向,或者如何更改我的代码以解决这个障碍。 我希望得到你的帮助。

@Override protected Boolean doInBackground(Void...param) { Log.d("url in NetworkTask", url); HttpGet httpGet = new HttpGet(url); AndroidHttpClient httpClient = AndroidHttpClient .newInstance("Android"); HttpResponse httpResponse = null; try { httpResponse = httpClient.execute(httpGet); } catch (IOException e) { e.printStackTrace(); } if(httpResponse!=null) primaryCodeStatus = httpResponse.getStatusLine() .getStatusCode(); else Log.d("httpResponse", "NULL"); if(primaryCodeStatus != 0){ try { urlFavIcon = new URL(url).getProtocol() +"://" +new URL(url).getHost() +"/favicon.ico"; fileName = new URL(url).getHost()+"(FAVICON).jpg"; System.out.println(urlFavIcon); } catch (MalformedURLException e) { e.printStackTrace(); } if(!(urlFavIcon.equals(""))){ httpGet = new HttpGet(urlFavIcon); try { httpResponse = httpClient.execute(httpGet); } catch (IOException e) { e.printStackTrace(); } try { is = (java.io.InputStream) httpResponse .getEntity() .getContent(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } httpClient.close(); Log.d("START SERVICE", "monitor url: "+url); return true; } else return false; } @Override public void onPostExecute(Boolean param){ if(param){ Bitmap siteIcon = BitmapFactory.decodeStream(is); String path = Environment.getExternalStorageDirectory().getPath()+"/"; System.out.println(Environment.getExternalStorageDirectory().canWrite()); File fileFavIcon = null; fileFavIcon = new File(path, fileName); if(fileFavIcon != null && siteIcon != null){ try { FileOutputStream fOS = new FileOutputStream(fileFavIcon); siteIcon.compress(Bitmap.CompressFormat.PNG, 100, fOS); fOS.flush(); fOS.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } getActivity() .startService(new Intent(getActivity(), ExService.class) .putExtra("UrlBox", new UrlBoxHelper(new UrlBox(url, 60000)))); FragmentDialogAddNewUrlConectActivity conectActivity = (FragmentDialogAddNewUrlConectActivity) getActivity(); conectActivity .fragmentDialogClickButtonListener(url, drawableFavIconUrl); } else { Toast toastInfo = Toast .makeText(getActivity().getApplicationContext(), "This link does not exist page", Toast.LENGTH_LONG); toastInfo.setGravity(Gravity.TOP, 0, 0); toastInfo.show(); } } 

编辑

在这里,他写道解决了我的问题:

 httpGet = new HttpGet(urlFavIcon); HttpParams params = new BasicHttpParams(); params.setParameter("http.protocol.handle-redirects",false); httpGet.setParams(params); httpResponse = httpClient.execute(httpGet); Header locationHeader = httpResponse.getFirstHeader("location"); if(locationHeader != null){ System.out.println(locationHeader.getValue());// locationHeader.getValue() get new link(redirect) urlFavIcon = locationHeader.getValue(); } 

您应该检查http请求的状态代码 。 3xx代码表示重定向。 所以你可以正确地对它们做出反应。