@Override public URI getUri() { try { // final url is only non-null when we follow redirects URL finalUrl = response().getFinalUrl(); return finalUrl == null ? url.toURI() : finalUrl.toURI(); } catch (URISyntaxException e) { throw new HttpResponseException(e, "Uri cannot be parsed: %s", e.getMessage()); } }
static Response parseResponse(HTTPResponse response, HTTPRequest creatingRequest) { // Response URL will be null if it is the same as the request URL. URL responseUrl = response.getFinalUrl(); String urlString = (responseUrl != null ? responseUrl : creatingRequest.getURL()).toString(); int status = response.getResponseCode(); List<HTTPHeader> fetchHeaders = response.getHeaders(); List<Header> headers = new ArrayList<Header>(fetchHeaders.size()); String contentType = "application/octet-stream"; for (HTTPHeader fetchHeader : fetchHeaders) { String name = fetchHeader.getName(); String value = fetchHeader.getValue(); if ("Content-Type".equalsIgnoreCase(name)) { contentType = value; } headers.add(new Header(name, value)); } TypedByteArray body = null; byte[] fetchBody = response.getContent(); if (fetchBody != null) { body = new TypedByteArray(contentType, fetchBody); } return new Response(urlString, status, "", headers, body); } }
public void handle(HTTPResponse response) throws Exception { URL finalURL = response.getFinalUrl(); Assert.assertEquals(getUrl(""), finalURL); } });
public void handle(HTTPResponse response) throws Exception { URL finalURL = response.getFinalUrl(); Assert.assertEquals(getUrl(""), finalURL); } });
@Test public void testFollowRedirectsExternal() throws Exception { final URL redirectUrl = new URL("http://google.com/"); final String expectedDestinationURLPrefix = "http://www.google."; FetchOptions options = FetchOptions.Builder.followRedirects(); HTTPRequest request = new HTTPRequest(redirectUrl, HTTPMethod.GET, options); URLFetchService service = URLFetchServiceFactory.getURLFetchService(); HTTPResponse response = service.fetch(request); String destinationUrl = response.getFinalUrl().toString(); assertTrue("Did not get redirected.", destinationUrl.startsWith(expectedDestinationURLPrefix)); }