추가: debug(), cookieHandler(), 수정: get(), post()

master
mjkhan21 4 weeks ago
parent 1b996ac057
commit 4ece563336

@ -4,6 +4,7 @@ import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.math.BigInteger; import java.math.BigInteger;
import java.net.Authenticator; import java.net.Authenticator;
import java.net.CookieHandler;
import java.net.ProxySelector; import java.net.ProxySelector;
import java.net.URI; import java.net.URI;
import java.net.URLEncoder; import java.net.URLEncoder;
@ -83,12 +84,21 @@ import cokr.xit.foundation.data.JSON;
* @author mjkhan * @author mjkhan
*/ */
public class WebClient { public class WebClient {
/**http .
*/
public static void debug() {
System.setProperty("jdk.httpclient.HttpClient.log", "errors,requests,headers,frames[:control:data:window:all],content,ssl,trace,channel,all");
}
private HttpClient.Version version = HttpClient.Version.HTTP_2; private HttpClient.Version version = HttpClient.Version.HTTP_2;
private Duration timeout = Duration.ofSeconds(30); private Duration timeout = Duration.ofSeconds(30);
private Authenticator authenticator; private Authenticator authenticator;
private SSLContext sslContext; private SSLContext sslContext;
private ProxySelector proxy; private ProxySelector proxy;
private Charset charset;
private CookieHandler cookieHandler;
private HttpClient.Redirect followRedirects = HttpClient.Redirect.NORMAL; private HttpClient.Redirect followRedirects = HttpClient.Redirect.NORMAL;
/**http . HTTP/2 /**http . HTTP/2
@ -136,6 +146,24 @@ public class WebClient {
return this; return this;
} }
public Charset charset() {
return Assert.ifEmpty(charset, StandardCharsets.UTF_8);
}
/** . UTF-8.
* @param charset
* @return WebClient
*/
public WebClient charset(String charset) {
this.charset = Charset.forName(charset);
return this;
}
public WebClient cookieHandler(CookieHandler cookieHandler) {
this.cookieHandler = cookieHandler;
return this;
}
/** redirect . HttpClient.Redirect.NORMAL. /** redirect . HttpClient.Redirect.NORMAL.
* @param redirect redirect * @param redirect redirect
* @return WebClient * @return WebClient
@ -145,7 +173,10 @@ public class WebClient {
return this; return this;
} }
private HttpClient client;
private HttpClient client() { private HttpClient client() {
if (client == null) {
HttpClient.Builder builder = HttpClient.newBuilder() HttpClient.Builder builder = HttpClient.newBuilder()
.version(version) .version(version)
.connectTimeout(timeout) .connectTimeout(timeout)
@ -158,7 +189,12 @@ public class WebClient {
if (proxy != null) if (proxy != null)
builder.proxy(proxy); builder.proxy(proxy);
return builder.build(); if (cookieHandler != null)
builder.cookieHandler(cookieHandler);
client = builder.build();
}
return client;
} }
/**"GET" . /**"GET" .
@ -169,6 +205,7 @@ public class WebClient {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> HttpResponse<T> get(Consumer<Request> configurer) { public <T> HttpResponse<T> get(Consumer<Request> configurer) {
Request req = new Request(); Request req = new Request();
req.charset = charset();
configurer.accept(req); configurer.accept(req);
HttpRequest hreq = req.get(); HttpRequest hreq = req.get();
@ -190,12 +227,12 @@ public class WebClient {
log().debug("Sending request:\n{} {}", hreq.method(), hreq.uri()); log().debug("Sending request:\n{} {}", hreq.method(), hreq.uri());
if (!async) { if (!async) {
HttpResponse<T> resp = client().send(hreq, bodyHandler); HttpResponse<T> resp = client().send(hreq, bodyHandler);
log().debug("Received response"); log().debug("{} response received", resp.statusCode());
return resp; return resp;
} else { } else {
CompletableFuture<HttpResponse<T>> future = client().sendAsync(hreq, bodyHandler); CompletableFuture<HttpResponse<T>> future = client().sendAsync(hreq, bodyHandler);
future.thenApply(resp -> { future.thenApply(resp -> {
log().debug("Received response"); log().debug("{} response received", resp.statusCode());
respHandler.accept(resp); respHandler.accept(resp);
return resp; return resp;
}); });
@ -215,6 +252,7 @@ public class WebClient {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public <T> HttpResponse<T> post(Consumer<Request> configurer) { public <T> HttpResponse<T> post(Consumer<Request> configurer) {
Request req = new Request(); Request req = new Request();
req.charset = charset();
configurer.accept(req); configurer.accept(req);
HttpRequest hreq = req.post(); HttpRequest hreq = req.post();
@ -247,6 +285,14 @@ public class WebClient {
} }
} }
public String toString(InputStream inputStream) {
try {
return new String(inputStream.readAllBytes(), charset());
} catch (Exception e) {
throw Assert.runtimeException(e);
}
}
/** http . /** http .
* <p>Request . * <p>Request .
* <ul><li>{@link #uri(String) uri}: uri</li> * <ul><li>{@link #uri(String) uri}: uri</li>
@ -295,7 +341,7 @@ public class WebClient {
private boolean private boolean
async, async,
download; download;
private Charset charset = StandardCharsets.UTF_8; private Charset charset;
private LinkedHashMap<String, String> headers; private LinkedHashMap<String, String> headers;
private LinkedHashMap<String, Object> keyValues; private LinkedHashMap<String, Object> keyValues;
@ -357,13 +403,14 @@ public class WebClient {
return this; return this;
} }
/** . UTF-8. /** .
* @param charset
* @return Request * @return Request
*/ */
public Request charset(Charset charset) { public Request noCache() {
this.charset = charset; return
return this; header("Cache-Control", "no-cache, no-store, must-revalidate")
.header("Pragma", "no-cache")
.header("Expires", "0");
} }
/** . false(). /** . false().

Loading…
Cancel
Save