Last active
January 8, 2018 04:16
-
-
Save AravindaM/896185ab89db646d3395f2734facaaad to your computer and use it in GitHub Desktop.
Sample Http2 Client - Java 9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import jdk.incubator.http.HttpClient; | |
import jdk.incubator.http.HttpRequest; | |
import jdk.incubator.http.HttpResponse; | |
import java.io.IOException; | |
import java.net.URI; | |
import java.net.URISyntaxException; | |
public class Http2client { | |
public static void main(String[] args) { | |
HttpRequest httpRequest = null; | |
HttpResponse<String> httpResponse = null; | |
HttpClient httpClient = HttpClient.newHttpClient(); //HTTP_2 | |
System.out.println("httpClient.version() : "+ httpClient.version()); | |
try { | |
//Get Request | |
httpRequest = HttpRequest | |
.newBuilder() | |
.uri(new URI("https://www.google.com/")).GET().build(); | |
//POST Request | |
httpRequest = HttpRequest | |
.newBuilder() | |
.uri(new URI("https://sampleURL.com")) | |
.POST(HttpRequest.BodyProcessor.fromString("{\n" + | |
" \"Key1\": \"value1\",\n" + | |
" \"Key2\": \"value2\",\n" | |
"}")) | |
.header("Content-Type","application/json") | |
.build(); | |
httpResponse = httpClient.send(httpRequest, HttpResponse.BodyHandler.asString()); | |
System.out.println(httpResponse.body()); | |
} catch (IOException | InterruptedException | URISyntaxException e) { | |
e.printStackTrace(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment