Here are the examples of the java api class java.net.http.HttpResponse taken from open source projects.
1. SmokeTest#test11()
Project: openjdk
File: SmokeTest.java
File: SmokeTest.java
// Chunked output stream static void test11(String target) throws Exception { System.out.print("test11: " + target); URI uri = new URI(target); FileInputStream file = new FileInputStream(smallFile.toFile()); HttpRequest.Builder builder = client.request(uri).body(HttpRequest.fromInputStream(file)); HttpResponse response = builder.POST().response(); if (response.statusCode() != 200) { throw new RuntimeException("Wrong response code"); } Path download = Paths.get("test11.txt"); download.toFile().delete(); response.body(HttpResponse.asFile(download)); if (Files.size(download) != Files.size(smallFile)) { System.out.println("Original size: " + Files.size(smallFile)); System.out.println("Downloaded size: " + Files.size(download)); throw new RuntimeException("Size mismatch"); } System.out.println(" OK"); }
2. SmokeTest#test5()
Project: openjdk
File: SmokeTest.java
File: SmokeTest.java
// 100 Continue: use echo target static void test5(String target, boolean fixedLen) throws Exception { System.out.print("test5: " + target); URI uri = new URI(target); String requestBody = generateString(12 * 1024 + 13); HttpRequest.Builder builder = client.request(uri).expectContinue(true).body(fromString(requestBody)); if (fixedLen) { builder.header("XFixed", "yes"); } HttpResponse response = builder.GET().response(); String body = response.body(asString()); if (!body.equals(requestBody)) { throw new RuntimeException("Body mismatch: expected [" + body + "], got [" + body + "]"); } System.out.println(" OK"); }
3. SmokeTest#test2()
Project: openjdk
File: SmokeTest.java
File: SmokeTest.java
// POST use echo to check reply static void test2(String s, String body) throws Exception { System.out.print("test2: " + s); URI uri = new URI(s); HttpResponse response = client.request(uri).body(fromString(body)).POST().response(); if (response.statusCode() != 200) { throw new RuntimeException("Expected 200, got [ " + response.statusCode() + " ]"); } String reply = response.body(asString()); if (!reply.equals(body)) { throw new RuntimeException("Body mismatch: expected [" + body + "], got [" + reply + "]"); } System.out.println(" OK"); }
4. RequestBodyTest#requestBodyTypes()
Project: openjdk
File: RequestBodyTest.java
File: RequestBodyTest.java
static void requestBodyTypes(final String target, final String requestType, final String responseType, final boolean isAsync) throws Exception { System.out.println("Running test_request_body_type " + requestType + " and response type " + responseType + " and sync=" + isAsync); URI uri = new URI(target); byte buf[]; String filename = smallFile.toFile().getAbsolutePath(); String fileContents = HttpUtils.getFileContent(filename); buf = fileContents.getBytes(); HttpRequest.Builder builder = HttpUtils.getHttpRequestBuilder(client, requestType, uri); HttpResponse response; if (!isAsync) { response = builder.GET().response(); } else { response = builder.GET().responseAsync().join(); } HttpUtils.checkResponse(response, requestType, responseType); System.out.println("OK"); }
5. SplitResponse#main()
Project: openjdk
File: SplitResponse.java
File: SplitResponse.java
public static void main(String[] args) throws Exception { server = new Server(0); URI uri = new URI(server.getURL()); server.start(); HttpRequest request; HttpResponse r; CompletableFuture<HttpResponse> cf1; for (int i = 0; i < responses.length; i++) { cf1 = HttpRequest.create(uri).GET().responseAsync(); String body = responses[i]; Server.Connection c = server.activity(); sendSplitResponse(response(body), c); r = cf1.get(); if (r.statusCode() != 200) throw new RuntimeException("Failed"); String rxbody = r.body(HttpResponse.asString()); System.out.println("received " + rxbody); if (!rxbody.equals(body)) throw new RuntimeException("Failed"); c.close(); } HttpClient.getDefault().executorService().shutdownNow(); System.out.println("OK"); }
6. SmokeTest#test8()
Project: openjdk
File: SmokeTest.java
File: SmokeTest.java
static void test8(String target, boolean fixedLen) throws Exception { System.out.print("test8: " + target); URI uri = new URI(target); HttpRequest.Builder builder = client.request(uri).body(noBody()); if (fixedLen) { builder.header("XFixed", "yes"); } HttpResponse response = builder.GET().response(); StringBuilder sb = new StringBuilder(); InputStream is = response.body(asInputStream()); int c; byte[] buf = new byte[2048]; while ((c = is.read(buf)) != -1) { for (int i = 0; i < c; i++) sb.append((char) buf[i]); } is.close(); String body = sb.toString(); if (!body.equals("This is foo.txt\r\n")) { throw new RuntimeException("Expected \"This is foo.txt\", got: " + body); } System.out.println(" OK"); }
7. SmokeTest#test6()
Project: openjdk
File: SmokeTest.java
File: SmokeTest.java
// use echo static void test6(String target, boolean fixedLen) throws Exception { System.out.print("test6: " + target); URI uri = new URI(target); String requestBody = generateString(12 * 1024 + 3); HttpRequest.Builder builder = client.request(uri).body(noBody()); if (fixedLen) { builder.header("XFixed", "yes"); } HttpResponse response = builder.GET().response(); if (response.statusCode() != 200) { throw new RuntimeException("Expected 200, got [ " + response.statusCode() + " ]"); } String responseBody = response.body(asString()); if (responseBody.equals(requestBody)) { throw new RuntimeException("Body mismatch: expected [" + requestBody + "], got [" + responseBody + "]"); } System.out.println(" OK"); }
8. SmokeTest#test3()
Project: openjdk
File: SmokeTest.java
File: SmokeTest.java
// Redirect static void test3(String s) throws Exception { System.out.print("test3: " + s); URI uri = new URI(s); RedirectHandler handler = uri.getScheme().equals("https") ? redirectHandlerSecure : redirectHandler; HttpResponse response = client.request(uri).body(noBody()).GET().response(); if (response.statusCode() != 200) { throw new RuntimeException("Expected 200, got [ " + response.statusCode() + " ]"); } else { response.body(HttpResponse.asFile(Paths.get("redir1.txt"))); } Path downloaded = Paths.get("redir1.txt"); if (Files.size(downloaded) != Files.size(midSizedFile)) { throw new RuntimeException("Size mismatch"); } System.out.printf(" (count: %d) ", handler.count()); // repeat with async api handler.reset(); response = client.request(uri).body(noBody()).GET().responseAsync().join(); if (response.statusCode() != 200) { throw new RuntimeException("Expected 200, got [ " + response.statusCode() + " ]"); } else { response.body(HttpResponse.asFile(Paths.get("redir2.txt"))); } downloaded = Paths.get("redir2.txt"); if (Files.size(downloaded) != Files.size(midSizedFile)) { throw new RuntimeException("Size mismatch 2"); } System.out.printf(" (count: %d) ", handler.count()); System.out.println(" OK"); }
9. SmokeTest#test1()
Project: openjdk
File: SmokeTest.java
File: SmokeTest.java
// Basic test static void test1(String target, boolean fixedLen) throws Exception { System.out.print("test1: " + target); URI uri = new URI(target); HttpRequest.Builder builder = client.request(uri).body(noBody()); if (fixedLen) { builder.header("XFixed", "yes"); } HttpResponse response = builder.GET().response(); String body = response.body(asString()); if (!body.equals("This is foo.txt\r\n")) { throw new RuntimeException(); } // repeat async response = builder.GET().responseAsync().join(); body = response.body(asString()); if (!body.equals("This is foo.txt\r\n")) { throw new RuntimeException(); } System.out.println(" OK"); }
10. QuickResponses#main()
Project: openjdk
File: QuickResponses.java
File: QuickResponses.java
public static void main(String[] args) throws Exception { server = new Server(0); URI uri = new URI(server.getURL()); server.start(); HttpRequest request = HttpRequest.create(uri).GET(); CompletableFuture<HttpResponse> cf1 = request.responseAsync(); Server.Connection s1 = server.activity(); s1.send(entireResponse()); HttpResponse r = cf1.join(); if (r.statusCode() != 200 || !r.body(HttpResponse.asString()).equals(responses[0])) throw new RuntimeException("Failed on first response"); //now get the same identical response, synchronously to ensure same connection int remaining = responses.length - 1; for (int i = 0; i < remaining; i++) { r = HttpRequest.create(uri).GET().response(); if (r.statusCode() != 200) throw new RuntimeException("Failed"); String body = r.body(HttpResponse.asString()); if (!body.equals(responses[i + 1])) throw new RuntimeException("Failed"); } HttpClient.getDefault().executorService().shutdownNow(); System.out.println("OK"); }