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