add headers ex

This commit is contained in:
Loredana Crusoveanu 2017-12-01 16:11:07 +02:00
parent d4715a7329
commit bdf881701c
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 {
httpGetRequest();
httpPosttRequest();
httpPostRequest();
asynchronousRequest();
asynchronousMultipleRequests();
}
@ -36,14 +36,15 @@ public class HttpClientExample {
public static void httpGetRequest() throws URISyntaxException, IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
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());
String responseBody = response.body();
int responseStatusCode = response.statusCode();
System.out.println(responseBody);
}
public static void httpPosttRequest() throws URISyntaxException, IOException, InterruptedException {
public static void httpPostRequest() throws URISyntaxException, IOException, InterruptedException {
HttpClient client = HttpClient
.newBuilder()
.build();