Add HttpClient
This commit is contained in:
@@ -1,7 +1,29 @@
|
||||
package step1;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.URI;
|
||||
import java.net.http.HttpClient;
|
||||
import java.net.http.HttpRequest;
|
||||
import java.net.http.HttpResponse;
|
||||
import java.util.Base64;
|
||||
|
||||
public class Main {
|
||||
public static void main(String[] args){
|
||||
System.out.println("Hello World");
|
||||
|
||||
try(HttpClient client = HttpClient.newHttpClient()){
|
||||
String msg = "Hello !";
|
||||
byte[] encodedMsg = Base64.getEncoder().encode(msg.getBytes());
|
||||
HttpRequest request = HttpRequest.newBuilder()
|
||||
.uri(URI.create("http://127.0.0.1:8888"))
|
||||
.POST(HttpRequest.BodyPublishers.ofByteArray(encodedMsg))
|
||||
.build();
|
||||
|
||||
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
|
||||
|
||||
System.out.println("Status code: " + response.statusCode());
|
||||
System.out.println("Response body: " + response.body());
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user