Last active
March 28, 2024 17:58
-
-
Save rgolangh/b949d8617709d10ba6c690863e52f259 to your computer and use it in GitHub Desktop.
simple curl in java
This file contains hidden or 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 java.util.stream.*; | |
| import java.net.http.HttpRequest; | |
| import java.net.http.HttpClient; | |
| import java.net.URI; | |
| import java.net.http.HttpResponse; | |
| import java.net.http.HttpClient.Version; | |
| import java.net.http.HttpRequest.BodyPublishers; | |
| import java.net.http.HttpResponse.BodyHandlers; | |
| import java.time.Duration; | |
| import java.util.List; | |
| /* Just a curl-like program for debugging purposes */ | |
| class Main { | |
| public static void main(String... args) { | |
| var url = "https://example.org"; | |
| System.out.println("args is " + args); | |
| if (args.length == 0) { | |
| System.out.println("info - no args passed"); | |
| System.err.println("err -no args passed"); | |
| } else { | |
| url = args[0]; | |
| } | |
| System.out.println("test https client send with self signed cert"); | |
| HttpClient client = HttpClient.newBuilder().version(Version.HTTP_1_1).build(); | |
| // k8s control plane will send a goaway if we're using java's http 2 client | |
| // Maybe the ciphers java has get rejected? fallbacking to http 1.1 | |
| HttpRequest request = HttpRequest.newBuilder() | |
| .method("GET", BodyPublishers.noBody()) | |
| .uri(URI.create(url)).build(); | |
| HttpResponse<String> response; | |
| try { | |
| response = client.send(request, BodyHandlers.ofString()); | |
| System.out.println(response.statusCode()); | |
| System.out.println(response.body()); | |
| } catch (Exception e) { | |
| // TODO: handle exception | |
| System.out.println(e); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment