Skip to content

Commit 2ade3b8

Browse files
Http Api Example
1 parent fbfe7ba commit 2ade3b8

File tree

10 files changed

+665
-0
lines changed

10 files changed

+665
-0
lines changed

HttpApiExample/.idea/kotlinc.xml

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

HttpApiExample/.idea/misc.xml

Lines changed: 6 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

HttpApiExample/.idea/modules.xml

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

HttpApiExample/.idea/workspace.xml

Lines changed: 489 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

HttpApiExample/HttpApiExample.iml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>
Binary file not shown.
Binary file not shown.
204 Bytes
Binary file not shown.
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
module javadeveloperzone.base{
2+
requires jdk.incubator.httpclient;
3+
}

0 commit comments

Comments
 (0)