commit
bec94e9508
1
core-java-9/run-httpclient.bat
Normal file
1
core-java-9/run-httpclient.bat
Normal file
@ -0,0 +1 @@
|
|||||||
|
java --module-path mods -m com.baeldung.httpclient/com.baeldung.httpclient.HttpClientExample
|
@ -28,7 +28,7 @@ public class HttpClientExample {
|
|||||||
|
|
||||||
public static void main(String[] args) throws Exception {
|
public static void main(String[] args) throws Exception {
|
||||||
httpGetRequest();
|
httpGetRequest();
|
||||||
httpPosttRequest();
|
httpPostRequest();
|
||||||
asynchronousRequest();
|
asynchronousRequest();
|
||||||
asynchronousMultipleRequests();
|
asynchronousMultipleRequests();
|
||||||
}
|
}
|
||||||
@ -36,14 +36,15 @@ public class HttpClientExample {
|
|||||||
public static void httpGetRequest() throws URISyntaxException, IOException, InterruptedException {
|
public static void httpGetRequest() throws URISyntaxException, IOException, InterruptedException {
|
||||||
HttpClient client = HttpClient.newHttpClient();
|
HttpClient client = HttpClient.newHttpClient();
|
||||||
URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1");
|
URI httpURI = new URI("http://jsonplaceholder.typicode.com/posts/1");
|
||||||
HttpRequest request = HttpRequest.newBuilder(httpURI).GET().build();
|
HttpRequest request = HttpRequest.newBuilder(httpURI).GET()
|
||||||
|
.headers("Accept-Enconding", "gzip, deflate").build();
|
||||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandler.asString());
|
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandler.asString());
|
||||||
String responseBody = response.body();
|
String responseBody = response.body();
|
||||||
int responseStatusCode = response.statusCode();
|
int responseStatusCode = response.statusCode();
|
||||||
System.out.println(responseBody);
|
System.out.println(responseBody);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void httpPosttRequest() throws URISyntaxException, IOException, InterruptedException {
|
public static void httpPostRequest() throws URISyntaxException, IOException, InterruptedException {
|
||||||
HttpClient client = HttpClient
|
HttpClient client = HttpClient
|
||||||
.newBuilder()
|
.newBuilder()
|
||||||
.build();
|
.build();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user