추가: 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.math.BigInteger;
import java.net.Authenticator;
import java.net.CookieHandler;
import java.net.ProxySelector;
import java.net.URI;
import java.net.URLEncoder;
@ -83,12 +84,21 @@ import cokr.xit.foundation.data.JSON;
* @author mjkhan
*/
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 Duration timeout = Duration.ofSeconds(30);
private Authenticator authenticator;
private SSLContext sslContext;
private ProxySelector proxy;
private Charset charset;
private CookieHandler cookieHandler;
private HttpClient.Redirect followRedirects = HttpClient.Redirect.NORMAL;
/**http . HTTP/2
@ -136,6 +146,24 @@ public class WebClient {
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.
* @param redirect redirect
* @return WebClient
@ -145,20 +173,28 @@ public class WebClient {
return this;
}
private HttpClient client;
private HttpClient client() {
HttpClient.Builder builder = HttpClient.newBuilder()
.version(version)
.connectTimeout(timeout)
.followRedirects(followRedirects);
if (authenticator != null)
builder.authenticator(authenticator);
if (sslContext != null)
builder.sslContext(sslContext);
if (proxy != null)
builder.proxy(proxy);
return builder.build();
if (client == null) {
HttpClient.Builder builder = HttpClient.newBuilder()
.version(version)
.connectTimeout(timeout)
.followRedirects(followRedirects);
if (authenticator != null)
builder.authenticator(authenticator);
if (sslContext != null)
builder.sslContext(sslContext);
if (proxy != null)
builder.proxy(proxy);
if (cookieHandler != null)
builder.cookieHandler(cookieHandler);
client = builder.build();
}
return client;
}
/**"GET" .
@ -169,6 +205,7 @@ public class WebClient {
@SuppressWarnings("unchecked")
public <T> HttpResponse<T> get(Consumer<Request> configurer) {
Request req = new Request();
req.charset = charset();
configurer.accept(req);
HttpRequest hreq = req.get();
@ -190,12 +227,12 @@ public class WebClient {
log().debug("Sending request:\n{} {}", hreq.method(), hreq.uri());
if (!async) {
HttpResponse<T> resp = client().send(hreq, bodyHandler);
log().debug("Received response");
log().debug("{} response received", resp.statusCode());
return resp;
} else {
CompletableFuture<HttpResponse<T>> future = client().sendAsync(hreq, bodyHandler);
future.thenApply(resp -> {
log().debug("Received response");
log().debug("{} response received", resp.statusCode());
respHandler.accept(resp);
return resp;
});
@ -215,6 +252,7 @@ public class WebClient {
@SuppressWarnings("unchecked")
public <T> HttpResponse<T> post(Consumer<Request> configurer) {
Request req = new Request();
req.charset = charset();
configurer.accept(req);
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 .
* <p>Request .
* <ul><li>{@link #uri(String) uri}: uri</li>
@ -295,7 +341,7 @@ public class WebClient {
private boolean
async,
download;
private Charset charset = StandardCharsets.UTF_8;
private Charset charset;
private LinkedHashMap<String, String> headers;
private LinkedHashMap<String, Object> keyValues;
@ -357,13 +403,14 @@ public class WebClient {
return this;
}
/** . UTF-8.
* @param charset
/** .
* @return Request
*/
public Request charset(Charset charset) {
this.charset = charset;
return this;
public Request noCache() {
return
header("Cache-Control", "no-cache, no-store, must-revalidate")
.header("Pragma", "no-cache")
.header("Expires", "0");
}
/** . false().

Loading…
Cancel
Save