private static void printHeaders(CharacterHttpResponse response) { System.out.println(response.getStatusLine()); for (HttpHeader next : response.getHeaders()) { System.out.printf("%s: %s\n", next.getName(), next.getValue()); } }
private static void request(String host, String path) throws UnknownHostException, IOException { final HttpGetExample example = new HttpGetExample(); final CharacterHttpResponse response = example.createRequest(host, HTTP_METHOD_GET, path); String statusLine = new String(response.getStatusLine()); final int statusCode = getStatusCode(statusLine); if (statusCode != 200) { final String location = getLocationFromHeader(response); if (location != null) { request(location.substring(0, location.length() - 1).replace("http://", ""), "/"); } } else { printHeaders(response); } }
private CharacterHttpResponse parseResponse(BufferedReader in) throws IOException { final CharacterHttpResponse result = new CharacterHttpResponse(); result.setStatusLine(in.readLine()); String next; while (!(next = in.readLine()).equals("")) { result.getHeaders().add(HttpHeader.createFromHeaderLine(next)); } // TODO chunked transfer-encoding is not supported! // reading body - we already know how many bytes the body is // (from the content-length header line) in.read(result.getBody()); return result; }
private static String getLocationFromHeader(CharacterHttpResponse response) { for (HttpHeader next : response.getHeaders()) { if (next.getName().contains("Location")) { return next.getValue(); } } return null; }