use of java.net.http.HttpRequest in project eclipse.jdt.ls by eclipse.
the class MavenCentralIdentifier method find.
private ArtifactKey find(String searchUrl, IProgressMonitor monitor) throws IOException, InterruptedException {
Duration timeout = Duration.ofSeconds(10);
HttpClient client = HttpClient.newBuilder().connectTimeout(timeout).proxy(ProxySelector.getDefault()).version(Version.HTTP_2).build();
HttpRequest httpRequest = HttpRequest.newBuilder().timeout(timeout).uri(URI.create(searchUrl)).GET().build();
if (monitor == null) {
monitor = new NullProgressMonitor();
}
if (monitor.isCanceled()) {
return null;
}
// TODO implement request cancellation, according to monitor status
HttpResponse<String> response = client.send(httpRequest, HttpResponse.BodyHandlers.ofString());
JsonElement jsonElement = new JsonParser().parse(response.body());
if (jsonElement != null && jsonElement.isJsonObject()) {
return extractKey(jsonElement.getAsJsonObject());
}
return null;
}
use of java.net.http.HttpRequest in project weicoder by wdcode.
the class Jdk11Http method post.
@Override
public String post(String url, Map<String, Object> data, Map<String, Object> header) {
try {
// 请求body
String body = S.add("?", S.toParameters(data));
// // 判断有参数提交
// if (U.E.isNotEmpty(data)) {
// // 声明字符串缓存
// StringBuilder sb = new StringBuilder("?");
// // 循环参数
// data.entrySet().forEach(e -> sb.append(e.getKey()).append("=").append(e.getValue()).append("&"));
// body = sb.substring(0, sb.length() - 1);
// }
// 获得HttpRequest构建器
HttpRequest.Builder builder = HttpRequest.newBuilder(URI.create(url + body));
// 头不为空,添加头
if (U.E.isNotEmpty(header))
header.forEach((k, v) -> builder.setHeader(k, W.C.toString(v)));
// HttpRequest
HttpRequest request = builder.POST(BodyPublishers.noBody()).build();
// 请求
HttpResponse<String> response = CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
// Logs.debug("Jdk11Http post url={} data={} header={} res={}", url, data, header, res);
return response.body();
} catch (Exception e) {
Logs.error(e, "Jdk11Http post url={} data={} header={}", url, data, header);
}
return C.S.EMPTY;
}
use of java.net.http.HttpRequest in project weicoder by wdcode.
the class Jdk11Http method download.
@Override
public byte[] download(String url, Map<String, Object> header) {
try {
// 获得HttpRequest构建器
HttpRequest.Builder builder = HttpRequest.newBuilder(URI.create(url));
// 头不为空,添加头
if (U.E.isNotEmpty(header))
for (Map.Entry<String, Object> h : header.entrySet()) builder.setHeader(h.getKey(), W.C.toString(h.getValue()));
// HttpRequest
HttpRequest request = builder.GET().build();
// 请求
HttpResponse<byte[]> response = CLIENT.send(request, HttpResponse.BodyHandlers.ofByteArray());
// 返回结果
return response.body();
} catch (Exception e) {
Logs.error(e, "Jdk11Http download url={}", url);
}
return C.A.BYTES_EMPTY;
}
Aggregations