|
| 1 | +packageexample; |
| 2 | + |
| 3 | +importjdk.incubator.http.HttpClient; |
| 4 | +importjdk.incubator.http.HttpRequest; |
| 5 | +importjdk.incubator.http.HttpResponse; |
| 6 | + |
| 7 | +importjavax.net.ssl.SSLParameters; |
| 8 | +importjava.io.IOException; |
| 9 | +importjava.net.Authenticator; |
| 10 | +importjava.net.PasswordAuthentication; |
| 11 | +importjava.net.URI; |
| 12 | +importjava.net.URISyntaxException; |
| 13 | +importjava.nio.file.Path; |
| 14 | +importjava.nio.file.Paths; |
| 15 | +importjava.util.concurrent.CompletableFuture; |
| 16 | +importjava.util.concurrent.ExecutionException; |
| 17 | + |
| 18 | +/** |
| 19 | + * Created by nitin on 4/9/2017. |
| 20 | + */ |
| 21 | +publicclassHttpApiDemo{ |
| 22 | + |
| 23 | +publicvoidsaveHttpResponseAsFile(){ |
| 24 | +try{ |
| 25 | +HttpClientclient = HttpClient.newHttpClient(); |
| 26 | + |
| 27 | +HttpRequestrequest = HttpRequest.newBuilder() |
| 28 | + .uri(newURI("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 | +PathtempFile = 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 (URISyntaxExceptione){ |
| 42 | +e.printStackTrace(); |
| 43 | + } catch (InterruptedExceptione){ |
| 44 | +e.printStackTrace(); |
| 45 | + } catch (IOExceptione){ |
| 46 | +e.printStackTrace(); |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | +publicvoidgetHttpResponseAsString(){ |
| 51 | +try{ |
| 52 | +HttpClientclient = HttpClient.newHttpClient(); |
| 53 | + |
| 54 | +HttpRequestrequest = HttpRequest.newBuilder() |
| 55 | + .uri(newURI("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 | +SSLParameterssslParameters = strResponse.sslParameters(); |
| 64 | +System.out.println("Maximum packet size : "+sslParameters.getMaximumPacketSize()); |
| 65 | + |
| 66 | +//System.out.println(response.body()); |
| 67 | + } catch (URISyntaxExceptione){ |
| 68 | +e.printStackTrace(); |
| 69 | + } catch (InterruptedExceptione){ |
| 70 | +e.printStackTrace(); |
| 71 | + } catch (IOExceptione){ |
| 72 | +e.printStackTrace(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | +publicvoidsendAsynchronousRequest(){ |
| 77 | +try{ |
| 78 | +HttpClientclient = HttpClient.newHttpClient(); |
| 79 | + |
| 80 | +HttpRequestrequest = HttpRequest.newBuilder() |
| 81 | + .uri(newURI("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 (URISyntaxExceptione){ |
| 100 | +e.printStackTrace(); |
| 101 | + } catch (InterruptedExceptione){ |
| 102 | +e.printStackTrace(); |
| 103 | + } catch (ExecutionExceptione){ |
| 104 | +e.printStackTrace(); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | +publicvoidsetBasicAuth(){ |
| 109 | +try{ |
| 110 | +HttpClientclient = HttpClient.newBuilder() |
| 111 | + .authenticator(newAuthenticator(){ |
| 112 | +@Override |
| 113 | +protectedPasswordAuthenticationgetPasswordAuthentication(){ |
| 114 | +returnnewPasswordAuthentication("username", "password".toCharArray()); |
| 115 | + } |
| 116 | + }) |
| 117 | + .build(); |
| 118 | + |
| 119 | +HttpRequestrequest = HttpRequest.newBuilder() |
| 120 | + .uri(newURI("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 (InterruptedExceptione){ |
| 128 | +e.printStackTrace(); |
| 129 | + } catch (IOExceptione){ |
| 130 | +e.printStackTrace(); |
| 131 | + } catch (URISyntaxExceptione){ |
| 132 | +e.printStackTrace(); |
| 133 | + } |
| 134 | + } |
| 135 | +publicstaticvoidmain(String[] args){ |
| 136 | + |
| 137 | +HttpApiDemoapiDemo = newHttpApiDemo(); |
| 138 | +apiDemo.sendAsynchronousRequest(); |
| 139 | + |
| 140 | + } |
| 141 | +} |
0 commit comments