Merge pull request #3174 from eugenp/BAEL-21

add headers ex
This commit is contained in:
Loredana Crusoveanu 2017-12-01 17:05:50 +02:00 committed by GitHub
commit bec94e9508
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 5 additions and 3 deletions

View File

@ -0,0 +1 @@
java --module-path mods -m com.baeldung.httpclient/com.baeldung.httpclient.HttpClientExample

View File

@ -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();