File name
Commit message
Commit date
File name
Commit message
Commit date
package logApi;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpHeaders;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.charset.Charset;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
/**
* Java 9 introduced a new incubating HttpClient API for dealing with HTTP requests.
* As of Java 11 this API is now final and available in the standard libraries package java.net. Let's explore what we can do with this API.
*/
public class LogAPI {
/**
* Example for sending a synchronous GET request
*
* @throws java.io.IOException
* @throws InterruptedException
*/
private static void demo1() throws java.io.IOException, InterruptedException {
System.out.println("Demo 1");
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://signus-mesweb.koreacentral.cloudapp.azure.com/api/user"))
.header("Content-Type", "text/plain")
.GET()
.build();
/*
The new HttpClient can be used either synchronously or asynchronously.
A /synchronous/ request blocks the current thread until the response is available.
BodyHandlers define the expected type of response body (e.g. as string, byte-array or file):
*/
var client = HttpClient.newHttpClient();
HttpResponse.BodyHandler<String> asString = HttpResponse.BodyHandlers.ofString();
/*
* HttpResponse.BodyHandlers
* .olLines() | .ofByteArray() | ofFile() | ofFileDownload()
*/
HttpResponse<String> response = client.send(request, asString);
int statusCode = response.statusCode();
System.out.printf("Status Code: %s%n", statusCode);
HttpHeaders headers = response.headers();
System.out.printf("Response Headers: %s%n", headers);
System.out.println(response.body());
}
/**
* Example for sending an asynchronous GET request
*
* @throws InterruptedException
* @throws java.util.concurrent.ExecutionException
*/
private static void demo2() throws InterruptedException, java.util.concurrent.ExecutionException {
System.out.println("Demo 2");
var request = HttpRequest.newBuilder()
.uri(URI.create("https://example.com"))
// .GET() // can be omitted as it is the default...
.build();
/*
* A request can also be performed asynchronously.
* Calling sendAsync does not block the current thread and instead returns a
* CompletableFuture to construct asynchronous operation pipelines.
*/
var client = HttpClient.newHttpClient();
CompletableFuture<HttpResponse<String>> responseFuture = //
client.sendAsync(request, HttpResponse.BodyHandlers.ofString());
responseFuture
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.get() // wait for result
;
}
/**
* Example for sending an asynchronous POST request
*
* @throws InterruptedException
* @throws java.util.concurrent.ExecutionException
*/
private static void demo3() throws ExecutionException, InterruptedException {
System.out.println("Demo 3");
var postRequest = HttpRequest.newBuilder()
.uri(URI.create("https://signus-mesweb.koreacentral.cloudapp.azure.com/api/user"))
.header("Content-Type", "text/plain")
.POST(HttpRequest.BodyPublishers.ofString("Hi there!"))
.build();
ExecutorService executor = Executors.newSingleThreadExecutor();
var client = HttpClient.newBuilder().executor(executor).build();
var responseFuture = client.sendAsync(postRequest, HttpResponse.BodyHandlers.ofString());
responseFuture.thenApply(res -> {
System.out.printf("StatusCode: %s%n", res.statusCode());
return res;
})
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.get();
executor.shutdownNow();
}
public static void send(String body) throws ExecutionException, InterruptedException {
System.out.println("Demo 3:"+HttpRequest.BodyPublishers.ofString(body));
var postRequest = HttpRequest.newBuilder()
.uri(URI.create("https://log.smart-factory.kr/apisvc/sendLogDataJSON.do"))
.header("Content-Type", "application/x-www-form-urlencoded")
.POST(HttpRequest.BodyPublishers.ofString(body, Charset.forName("UTF-8")))
.build();
ExecutorService executor = Executors.newSingleThreadExecutor();
var client = HttpClient.newBuilder().executor(executor).build();
var responseFuture = client.sendAsync(postRequest, HttpResponse.BodyHandlers.ofString());
responseFuture.thenApply(res -> {
System.out.printf("StatusCode: %s%n", res.statusCode());
return res;
})
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.get();
executor.shutdownNow();
}
public static void main(String args[]) {
new DBConnection();
}
}