Search in sources :

Example 36 with HttpRequest

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;
}
Also used : HttpRequest(java.net.http.HttpRequest) NullProgressMonitor(org.eclipse.core.runtime.NullProgressMonitor) JsonElement(com.google.gson.JsonElement) HttpClient(java.net.http.HttpClient) Duration(java.time.Duration) JsonParser(com.google.gson.JsonParser)

Example 37 with HttpRequest

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;
}
Also used : HttpRequest(java.net.http.HttpRequest) BaseHttp(com.weicoder.common.http.base.BaseHttp) Version(java.net.http.HttpClient.Version) BodyPublishers(java.net.http.HttpRequest.BodyPublishers) S(com.weicoder.common.U.S) W(com.weicoder.common.W) U(com.weicoder.common.U) HttpRequest(java.net.http.HttpRequest) C(com.weicoder.common.C) Map(java.util.Map) HttpClient(java.net.http.HttpClient) URI(java.net.URI) Logs(com.weicoder.common.log.Logs) HttpResponse(java.net.http.HttpResponse)

Example 38 with HttpRequest

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;
}
Also used : HttpRequest(java.net.http.HttpRequest)

Aggregations

HttpRequest (java.net.http.HttpRequest)38 InputStream (java.io.InputStream)15 HttpClient (java.net.http.HttpClient)10 TypedInputStream (org.apache.jena.atlas.web.TypedInputStream)10 URI (java.net.URI)9 IOException (java.io.IOException)7 HttpResponse (java.net.http.HttpResponse)7 Duration (java.time.Duration)6 Test (org.junit.Test)5 HttpException (org.apache.jena.atlas.web.HttpException)4 SerializedClassRunner (io.pravega.test.common.SerializedClassRunner)3 TestUtils (io.pravega.test.common.TestUtils)3 URISyntaxException (java.net.URISyntaxException)3 Pattern (java.util.regex.Pattern)3 Cleanup (lombok.Cleanup)3 Assert.assertTrue (org.junit.Assert.assertTrue)3 RunWith (org.junit.runner.RunWith)3 JsonParser (com.google.gson.JsonParser)2 Counter (io.pravega.shared.metrics.Counter)2 MetricsConfig (io.pravega.shared.metrics.MetricsConfig)2