|
| 1 | +package example; |
| 2 | + |
| 3 | +import jdk.incubator.http.HttpClient; |
| 4 | +import jdk.incubator.http.HttpRequest; |
| 5 | +import jdk.incubator.http.HttpResponse; |
| 6 | + |
| 7 | +import javax.net.ssl.SSLParameters; |
| 8 | +import java.io.IOException; |
| 9 | +import java.net.Authenticator; |
| 10 | +import java.net.PasswordAuthentication; |
| 11 | +import java.net.URI; |
| 12 | +import java.net.URISyntaxException; |
| 13 | +import java.nio.file.Path; |
| 14 | +import java.nio.file.Paths; |
| 15 | +import java.util.concurrent.CompletableFuture; |
| 16 | +import java.util.concurrent.ExecutionException; |
| 17 | + |
| 18 | +/** |
| 19 | + * Created by nitin on 4/9/2017. |
| 20 | + */ |
| 21 | +public class HttpApiDemo { |
| 22 | + |
| 23 | + public void saveHttpResponseAsFile(){ |
| 24 | + try { |
| 25 | + HttpClient client = HttpClient.newHttpClient(); |
| 26 | + |
| 27 | + HttpRequest request = HttpRequest.newBuilder() |
| 28 | + .uri(new URI("http://javadeveloperzone.com/java-basic/java-9-features/java-9-module-example/")) |
| 29 | + .GET() |
| 30 | + .build(); |
| 31 | + |
| 32 | + //String body handler |
| 33 | + HttpResponse<String> strResponse = client.send(request, HttpResponse.BodyHandler.asString()); |
| 34 | + //Path tempFile = Files.createFile("test", ".html"); |
| 35 | + Path tempFile = Paths.get("G:\\study\\Blogs\\Applications\\Sample.html"); |
| 36 | + |
| 37 | + HttpResponse<Path> response = client.send(request, HttpResponse.BodyHandler.asFile(tempFile)); |
| 38 | + |
| 39 | + System.out.println(response.statusCode()); |
| 40 | + //System.out.println(response.body()); |
| 41 | + } catch (URISyntaxException e) { |
| 42 | + e.printStackTrace(); |
| 43 | + } catch (InterruptedException e) { |
| 44 | + e.printStackTrace(); |
| 45 | + } catch (IOException e) { |
| 46 | + e.printStackTrace(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + public void getHttpResponseAsString(){ |
| 51 | + try { |
| 52 | + HttpClient client = HttpClient.newHttpClient(); |
| 53 | + |
| 54 | + HttpRequest request = HttpRequest.newBuilder() |
| 55 | + .uri(new URI("http://javadeveloperzone.com/java-basic/java-9-features/java-9-module-example/")) |
| 56 | + .GET() |
| 57 | + .build(); |
| 58 | + |
| 59 | + //String body handler |
| 60 | + HttpResponse<String> strResponse = client.send(request, HttpResponse.BodyHandler.asString()); |
| 61 | + |
| 62 | + System.out.println(strResponse.statusCode()); |
| 63 | + SSLParameters sslParameters = strResponse.sslParameters(); |
| 64 | + System.out.println("Maximum packet size : "+sslParameters.getMaximumPacketSize()); |
| 65 | + |
| 66 | + //System.out.println(response.body()); |
| 67 | + } catch (URISyntaxException e) { |
| 68 | + e.printStackTrace(); |
| 69 | + } catch (InterruptedException e) { |
| 70 | + e.printStackTrace(); |
| 71 | + } catch (IOException e) { |
| 72 | + e.printStackTrace(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public void sendAsynchronousRequest(){ |
| 77 | + try { |
| 78 | + HttpClient client = HttpClient.newHttpClient(); |
| 79 | + |
| 80 | + HttpRequest request = HttpRequest.newBuilder() |
| 81 | + .uri(new URI("http://javadeveloperzone.com/java-basic/java-9-features/java-9-module-example/")) |
| 82 | + .GET() |
| 83 | + .build(); |
| 84 | + |
| 85 | + //String body handler |
| 86 | + CompletableFuture<HttpResponse<String>> strResponse = client.sendAsync(request, HttpResponse.BodyHandler.asString()); |
| 87 | + |
| 88 | + Thread.sleep(200); |
| 89 | + if(strResponse.isDone()){ |
| 90 | + System.out.println(strResponse.get().statusCode()); |
| 91 | + }else{ |
| 92 | + System.out.println("Request take more than 200 millisecons..."); |
| 93 | + strResponse.cancel(true); |
| 94 | + if(strResponse.isCancelled()){ |
| 95 | + System.out.println("request cancelled !!!"); |
| 96 | + } |
| 97 | + } |
| 98 | + //System.out.println(response.body()); |
| 99 | + } catch (URISyntaxException e) { |
| 100 | + e.printStackTrace(); |
| 101 | + } catch (InterruptedException e) { |
| 102 | + e.printStackTrace(); |
| 103 | + } catch (ExecutionException e) { |
| 104 | + e.printStackTrace(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + public void setBasicAuth(){ |
| 109 | + try { |
| 110 | + HttpClient client = HttpClient.newBuilder() |
| 111 | + .authenticator(new Authenticator() { |
| 112 | + @Override |
| 113 | + protected PasswordAuthentication getPasswordAuthentication() { |
| 114 | + return new PasswordAuthentication("username", "password".toCharArray()); |
| 115 | + } |
| 116 | + }) |
| 117 | + .build(); |
| 118 | + |
| 119 | + HttpRequest request = HttpRequest.newBuilder() |
| 120 | + .uri(new URI("http://javadeveloperzone.com/java-basic/java-9-features/java-9-module-example/")) |
| 121 | + .GET() |
| 122 | + .build(); |
| 123 | + |
| 124 | + HttpResponse<String> response = client.send(request, HttpResponse.BodyHandler.asString()); |
| 125 | + System.out.println(response.statusCode()); |
| 126 | + System.out.println(response.body()); |
| 127 | + } catch (InterruptedException e) { |
| 128 | + e.printStackTrace(); |
| 129 | + } catch (IOException e) { |
| 130 | + e.printStackTrace(); |
| 131 | + } catch (URISyntaxException e) { |
| 132 | + e.printStackTrace(); |
| 133 | + } |
| 134 | + } |
| 135 | + public static void main(String[] args) { |
| 136 | + |
| 137 | + HttpApiDemo apiDemo = new HttpApiDemo(); |
| 138 | + apiDemo.sendAsynchronousRequest(); |
| 139 | + |
| 140 | + } |
| 141 | +} |
0 commit comments