HttpUtils { public static IndependentPair<HttpEntity, ByteArrayInputStream> readURLAsByteArrayInputStream(URL u, boolean followRedirects) throws IOException { return readURLAsByteArrayInputStream(u, 15000, 15000, followRedirects ? new MetaRefreshRedirectStrategy() : null, DEFAULT_USERAGENT); } private  HttpUtils(); static byte[] readURLAsBytes(URL u); static byte[] readURLAsBytes(URL u, boolean followRedirects); static IndependentPair<HttpEntity, ByteArrayInputStream> readURLAsByteArrayInputStream(URL u,
			boolean followRedirects); static IndependentPair<HttpEntity, ByteArrayInputStream> readURLAsByteArrayInputStream(URL u,
			RedirectStrategy strategy); static IndependentPair<HttpEntity, ByteArrayInputStream> readURLAsByteArrayInputStream(URL url,
			int connectionTimeout, int readTimeout, RedirectStrategy redirectStrategy, String userAgent); static InputStream readURL(URL url); static InputStream readURL(URL url, boolean followRedirects); static InputStream readURLAsStream(URL url); static InputStream readURLAsStream(URL url, boolean followRedirects); static T readURL(URL url, T obj); static T readURL(URL url, Class<? extends T> clz); static T readURL(URL url, Q reader); static final String DEFAULT_USERAGENT; }
@Test public void testRedirect() throws MalformedURLException, IOException { final String[][] links = new String[][] { new String[] { "http: "http: "https: } }; for (final String[] link : links) { final String[] expecting = Arrays.copyOfRange(link, 1, link.length); final String firstLink = link[0]; HttpUtils.readURLAsByteArrayInputStream(new URL(firstLink), new HttpUtils.MetaRefreshRedirectStrategy() { int redirected = 0; @Override public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { final boolean isRedirect = super.isRedirected(request, response, context); if (redirected < expecting.length) { assertTrue(isRedirect); final HttpUriRequest redirect = this.getRedirect(request, response, context); final RedirectLocations redirectLocations = (RedirectLocations) context .getAttribute(REDIRECT_LOCATIONS); if (redirectLocations != null) redirectLocations.remove(redirect.getURI()); final String uriString = redirect.getURI().toString(); assertEquals(expecting[redirected++], uriString); } if (redirected == links.length) return false; return isRedirect; } }); } }