Skip to content

Instantly share code, notes, and snippets.

@AravindaM
Last active January 8, 2018 04:16
Show Gist options
  • Save AravindaM/896185ab89db646d3395f2734facaaad to your computer and use it in GitHub Desktop.
Save AravindaM/896185ab89db646d3395f2734facaaad to your computer and use it in GitHub Desktop.
Sample Http2 Client - Java 9
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