java.net.http.HttpRequest源码实例Demo

类java.net.http.HttpRequest源码实例Demo

下面列出了java.net.http.HttpRequest 类实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: r2cloud   文件: R2ServerClient.java
private void upload(String url, File file, String contentType) {
	HttpRequest request;
	try {
		request = createRequest(url).header("Content-Type", contentType).PUT(BodyPublishers.ofFile(file.toPath())).build();
	} catch (FileNotFoundException e) {
		LOG.error("unable to upload: {}", url, e);
		return;
	}
	httpclient.sendAsync(request, BodyHandlers.ofString()).exceptionally(ex -> {
		Util.logIOException(LOG, "unable to upload: " + url, ex);
		return null;
	}).thenAccept(response -> {
		if (response != null && response.statusCode() != 200 && LOG.isErrorEnabled()) {
			LOG.error("unable to upload: {} response code: {}. response: {}", url, response.statusCode(), response.body());
		}
	});
}
 
源代码2 项目: openapi-generator   文件: UserApi.java
/**
 * Logs out current logged in user session
 * 
 * @throws ApiException if fails to make API call
 */
public CompletableFuture<Void> logoutUser() throws ApiException {
  try {
    HttpRequest.Builder localVarRequestBuilder = logoutUserRequestBuilder();
    return memberVarHttpClient.sendAsync(
            localVarRequestBuilder.build(),
            HttpResponse.BodyHandlers.ofString()).thenComposeAsync(localVarResponse -> {
        if (localVarResponse.statusCode()/ 100 != 2) {
            return CompletableFuture.failedFuture(new ApiException(localVarResponse.statusCode(),
                "logoutUser call received non-success response",
                localVarResponse.headers(),
                localVarResponse.body())
            );
        } else {
            return CompletableFuture.completedFuture(null);
        }
    });
  }
  catch (ApiException e) {
    return CompletableFuture.failedFuture(e);
  }
}
 
源代码3 项目: piranha   文件: HttpServerTest.java
/**
 * Test processing.
 *
 * @throws Exception when an error occurs.
 */
@Test
public void testProcessing() throws Exception {
    HttpServer server = createServer(8765);
    server.start();
    try {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder(URI.create("http://localhost:8765")).build();
        HttpResponse<Void> response = client.send(request, HttpResponse.BodyHandlers.discarding());
        assertEquals(response.statusCode(), 200);
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    } finally {
        server.stop();
    }
}
 
源代码4 项目: openapi-generator   文件: StoreApi.java
private HttpRequest.Builder deleteOrderRequestBuilder(String orderId) throws ApiException {
  // verify the required parameter 'orderId' is set
  if (orderId == null) {
    throw new ApiException(400, "Missing the required parameter 'orderId' when calling deleteOrder");
  }

  HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

  String localVarPath = "/store/order/{order_id}"
      .replace("{order_id}", ApiClient.urlEncode(orderId.toString()));

  localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

  localVarRequestBuilder.header("Accept", "application/json");

  localVarRequestBuilder.method("DELETE", HttpRequest.BodyPublishers.noBody());
  if (memberVarReadTimeout != null) {
    localVarRequestBuilder.timeout(memberVarReadTimeout);
  }
  if (memberVarInterceptor != null) {
    memberVarInterceptor.accept(localVarRequestBuilder);
  }
  return localVarRequestBuilder;
}
 
源代码5 项目: java11-examples   文件: Example.java
public static void asyncPost() throws Exception {
    HttpClient client = HttpClient.newHttpClient();

    Gson gson = new Gson();
    Foo  foo  = new Foo();
    foo.name = "王爵nice";
    foo.url = "https://github.com/biezhi";

    String jsonBody = gson.toJson(foo);

    HttpRequest request = HttpRequest.newBuilder()
            .uri(new URI("https://httpbin.org/post"))
            .header("Content-Type", "application/json")
            .POST(HttpRequest.BodyPublishers.ofString(jsonBody))
            .build();
    client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
            .whenComplete((resp, t) -> {
                if (t != null) {
                    t.printStackTrace();
                } else {
                    System.out.println(resp.body());
                    System.out.println(resp.statusCode());
                }
            }).join();
}
 
源代码6 项目: openapi-generator   文件: FakeApi.java
/**
 * test inline additionalProperties
 * 
 * @param param request body (required)
 * @throws ApiException if fails to make API call
 */
public CompletableFuture<Void> testInlineAdditionalProperties(Map<String, String> param) throws ApiException {
  try {
    HttpRequest.Builder localVarRequestBuilder = testInlineAdditionalPropertiesRequestBuilder(param);
    return memberVarHttpClient.sendAsync(
            localVarRequestBuilder.build(),
            HttpResponse.BodyHandlers.ofString()).thenComposeAsync(localVarResponse -> {
        if (localVarResponse.statusCode()/ 100 != 2) {
            return CompletableFuture.failedFuture(new ApiException(localVarResponse.statusCode(),
                "testInlineAdditionalProperties call received non-success response",
                localVarResponse.headers(),
                localVarResponse.body())
            );
        } else {
            return CompletableFuture.completedFuture(null);
        }
    });
  }
  catch (ApiException e) {
    return CompletableFuture.failedFuture(e);
  }
}
 
源代码7 项目: alf.io   文件: RevolutBankTransferManager.java
private List<String> loadAccountsFromAPI(String revolutKey, String baseUrl) {
    var request = HttpRequest.newBuilder(URI.create(baseUrl + "/api/1.0/accounts"))
        .GET()
        .header("Authorization", "Bearer "+revolutKey)
        .build();
    try {
        var response = client.send(request, HttpResponse.BodyHandlers.ofString());
        if(response.statusCode() == HttpStatus.OK.value()) {
            List<Map<String, ?>> result = Json.fromJson(response.body(), new TypeReference<>() {});
            return result.stream().filter(m -> "active".equals(m.get("state"))).map(m -> (String) m.get("id")).collect(Collectors.toList());
        }
        throw new IllegalStateException("cannot retrieve accounts");
    } catch (Exception e) {
        log.warn("got error while retrieving accounts", e);
        throw new IllegalStateException(e);
    }
}
 
源代码8 项目: alf.io   文件: SimpleHttpClient.java
private SimpleHttpClientCachedResponse callRemoteAndSaveResponse(HttpRequest request) throws IOException {
    HttpResponse<InputStream> response = null;
    try {
        response = httpClient.send(request, HttpResponse.BodyHandlers.ofInputStream());
    } catch (InterruptedException exception) {
        Thread.currentThread().interrupt();
        logInterruption(exception);
    }
    Path tempFile = null;
    if (HttpUtils.callSuccessful(response)) {
        InputStream body = response.body();
        if (body != null) {
            tempFile = Files.createTempFile("extension-out", ".tmp");
            try (FileOutputStream out = new FileOutputStream(tempFile.toFile())) {
                body.transferTo(out);
            }
        }
    }
    return new SimpleHttpClientCachedResponse(
        HttpUtils.callSuccessful(response),
        response.statusCode(),
        response.headers().map(),
        tempFile != null ? tempFile.toAbsolutePath().toString() : null);
}
 
源代码9 项目: openapi-generator   文件: FakeApi.java
private HttpRequest.Builder fakeOuterStringSerializeRequestBuilder(String body) throws ApiException {

    HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

    String localVarPath = "/fake/outer/string";

    localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

    localVarRequestBuilder.header("Content-Type", "application/json");
    localVarRequestBuilder.header("Accept", "application/json");

    try {
      byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(body);
      localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody));
    } catch (IOException e) {
      throw new ApiException(e);
    }
    if (memberVarReadTimeout != null) {
      localVarRequestBuilder.timeout(memberVarReadTimeout);
    }
    if (memberVarInterceptor != null) {
      memberVarInterceptor.accept(localVarRequestBuilder);
    }
    return localVarRequestBuilder;
  }
 
@Test
@SuppressWarnings({"unchecked", "rawtypes"})
public void executeRequest() throws Exception {
    HttpResponse httpResponse = mock(HttpResponse.class);
    when(httpClient.send(any(HttpRequest.class), any(HttpResponse.BodyHandler.class))).thenReturn(httpResponse);
    when(httpResponse.statusCode()).thenReturn(200);
    when(httpResponse.body()).thenReturn("Hello");

    ArgumentCaptor<HttpRequest> httpRequestArgumentCaptor = ArgumentCaptor.forClass(HttpRequest.class);
    ArgumentCaptor<HttpResponse.BodyHandler<String>> bodyHandlerArgumentCaptor = ArgumentCaptor.forClass(HttpResponse.BodyHandler.class);

    ClientResponse clientResponse = victim.executeRequest(HTTP_URL);

    assertThat(clientResponse.getStatusCode()).isEqualTo(200);
    assertThat(clientResponse.getResponseBody()).isEqualTo("Hello");

    verify(httpClient, times(1)).send(httpRequestArgumentCaptor.capture(), bodyHandlerArgumentCaptor.capture());
    assertThat(httpRequestArgumentCaptor.getValue().uri().toString()).isEqualTo(HTTP_URL);
    assertThat(httpRequestArgumentCaptor.getValue().method()).isEqualTo(GET_METHOD);
    assertThat(httpRequestArgumentCaptor.getValue().headers().map()).containsExactly(Assertions.entry(HEADER_KEY_CLIENT_TYPE, Collections.singletonList(JDK_HTTP_CLIENT.getValue())));
    assertThat(bodyHandlerArgumentCaptor.getValue()).isEqualTo(HttpResponse.BodyHandlers.ofString());
}
 
源代码11 项目: openapi-generator   文件: PetApi.java
private HttpRequest.Builder updatePetWithFormRequestBuilder(Long petId, String name, String status) throws ApiException {
  // verify the required parameter 'petId' is set
  if (petId == null) {
    throw new ApiException(400, "Missing the required parameter 'petId' when calling updatePetWithForm");
  }

  HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

  String localVarPath = "/pet/{petId}"
      .replace("{petId}", ApiClient.urlEncode(petId.toString()));

  localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

  localVarRequestBuilder.header("Accept", "application/json");

  localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.noBody());
  if (memberVarReadTimeout != null) {
    localVarRequestBuilder.timeout(memberVarReadTimeout);
  }
  if (memberVarInterceptor != null) {
    memberVarInterceptor.accept(localVarRequestBuilder);
  }
  return localVarRequestBuilder;
}
 
源代码12 项目: openapi-generator   文件: FakeApi.java
private HttpRequest.Builder fakeOuterBooleanSerializeRequestBuilder(Boolean body) throws ApiException {

    HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

    String localVarPath = "/fake/outer/boolean";

    localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

    localVarRequestBuilder.header("Content-Type", "application/json");
    localVarRequestBuilder.header("Accept", "application/json");

    try {
      byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(body);
      localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody));
    } catch (IOException e) {
      throw new ApiException(e);
    }
    if (memberVarReadTimeout != null) {
      localVarRequestBuilder.timeout(memberVarReadTimeout);
    }
    if (memberVarInterceptor != null) {
      memberVarInterceptor.accept(localVarRequestBuilder);
    }
    return localVarRequestBuilder;
  }
 
源代码13 项目: openapi-generator   文件: UserApi.java
private HttpRequest.Builder deleteUserRequestBuilder(String username) throws ApiException {
  // verify the required parameter 'username' is set
  if (username == null) {
    throw new ApiException(400, "Missing the required parameter 'username' when calling deleteUser");
  }

  HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

  String localVarPath = "/user/{username}"
      .replace("{username}", ApiClient.urlEncode(username.toString()));

  localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

  localVarRequestBuilder.header("Accept", "application/json");

  localVarRequestBuilder.method("DELETE", HttpRequest.BodyPublishers.noBody());
  if (memberVarReadTimeout != null) {
    localVarRequestBuilder.timeout(memberVarReadTimeout);
  }
  if (memberVarInterceptor != null) {
    memberVarInterceptor.accept(localVarRequestBuilder);
  }
  return localVarRequestBuilder;
}
 
源代码14 项目: openapi-generator   文件: FakeApi.java
/**
 * To test enum parameters
 * To test enum parameters
 * @param enumHeaderStringArray Header parameter enum test (string array) (optional
 * @param enumHeaderString Header parameter enum test (string) (optional, default to -efg)
 * @param enumQueryStringArray Query parameter enum test (string array) (optional
 * @param enumQueryString Query parameter enum test (string) (optional, default to -efg)
 * @param enumQueryInteger Query parameter enum test (double) (optional)
 * @param enumQueryDouble Query parameter enum test (double) (optional)
 * @param enumFormStringArray Form parameter enum test (string array) (optional
 * @param enumFormString Form parameter enum test (string) (optional, default to -efg)
 * @throws ApiException if fails to make API call
 */
public CompletableFuture<Void> testEnumParameters(List<String> enumHeaderStringArray, String enumHeaderString, List<String> enumQueryStringArray, String enumQueryString, Integer enumQueryInteger, Double enumQueryDouble, List<String> enumFormStringArray, String enumFormString) throws ApiException {
  try {
    HttpRequest.Builder localVarRequestBuilder = testEnumParametersRequestBuilder(enumHeaderStringArray, enumHeaderString, enumQueryStringArray, enumQueryString, enumQueryInteger, enumQueryDouble, enumFormStringArray, enumFormString);
    return memberVarHttpClient.sendAsync(
            localVarRequestBuilder.build(),
            HttpResponse.BodyHandlers.ofString()).thenComposeAsync(localVarResponse -> {
        if (localVarResponse.statusCode()/ 100 != 2) {
            return CompletableFuture.failedFuture(new ApiException(localVarResponse.statusCode(),
                "testEnumParameters call received non-success response",
                localVarResponse.headers(),
                localVarResponse.body())
            );
        } else {
            return CompletableFuture.completedFuture(null);
        }
    });
  }
  catch (ApiException e) {
    return CompletableFuture.failedFuture(e);
  }
}
 
public CompletableFuture<List<CollectionTag>> request(String body) {
    HttpRequest httpRequest = HttpRequest.newBuilder()
            .header("Content-Type", "application/json")
            .uri(URI.create("http://" + elasticsearch + ":9200/collection_tag/_search"))
            .method("GET", HttpRequest.BodyPublishers.ofString(body))
            .build();
    return httpClient.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString()).thenApply(
            r -> {
                ElasticsearchResponse<CollectionTag> elasticsearchResponse;
                try {
                    if (r.body() != null) {
                        elasticsearchResponse = objectMapper.readValue(r.body(), new TypeReference<>() {
                        });
                        Hits<CollectionTag> hits = elasticsearchResponse.getHits();
                        if (hits != null && hits.getHits() != null) {
                            return hits.getHits().stream().map(Hit::getT).collect(Collectors.toList());
                            //return new SearchResult(hits.getTotal().getValue(), illustrationList);
                        }
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
                return null;
            }
    );
}
 
源代码16 项目: passpol   文件: HaveIBeenPwned.java
@Override
public boolean contains(String password) throws IOException {
  try {
    var hash = hex(MessageDigest.getInstance("SHA1").digest(PasswordPolicy.normalize(password)));
    var request =
        HttpRequest.newBuilder()
            .GET()
            .uri(BASE_URI.resolve(hash.substring(0, PREFIX_LENGTH)))
            .header("User-Agent", "passpol")
            .build();
    var response = client.send(request, BodyHandlers.ofLines());
    if (response.statusCode() != 200) {
      throw new IOException("Unexpected response from server: " + response.statusCode());
    }
    return response
        .body()
        .filter(s -> s.regionMatches(0, hash, PREFIX_LENGTH, SUFFIX_LENGTH))
        .map(s -> s.substring(HASH_LENGTH + DELIM_LENGTH))
        .mapToInt(Integer::parseInt)
        .anyMatch(t -> t >= threshold);
  } catch (NoSuchAlgorithmException | InterruptedException | IndexOutOfBoundsException e) {
    throw new IOException(e);
  }
}
 
源代码17 项目: openapi-generator   文件: StoreApi.java
/**
 * Delete purchase order by ID
 * For valid response try integer IDs with value &lt; 1000. Anything above 1000 or nonintegers will generate API errors
 * @param orderId ID of the order that needs to be deleted (required)
 * @throws ApiException if fails to make API call
 */
public CompletableFuture<Void> deleteOrder(String orderId) throws ApiException {
  try {
    HttpRequest.Builder localVarRequestBuilder = deleteOrderRequestBuilder(orderId);
    return memberVarHttpClient.sendAsync(
            localVarRequestBuilder.build(),
            HttpResponse.BodyHandlers.ofString()).thenComposeAsync(localVarResponse -> {
        if (localVarResponse.statusCode()/ 100 != 2) {
            return CompletableFuture.failedFuture(new ApiException(localVarResponse.statusCode(),
                "deleteOrder call received non-success response",
                localVarResponse.headers(),
                localVarResponse.body())
            );
        } else {
            return CompletableFuture.completedFuture(null);
        }
    });
  }
  catch (ApiException e) {
    return CompletableFuture.failedFuture(e);
  }
}
 
源代码18 项目: piranha   文件: DefaultHttpServerTest.java
/**
 * Test SO_TIMEOUT.
 */
@Test
public void testSoTimeout() {
    DefaultHttpServer server = new DefaultHttpServer(
            8765, new DefaultHttpServerProcessor(), 2000);
    assertEquals(server.getSoTimeout(), 2000);
    server.start();
    try {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder(URI.create("http://localhost:8765/")).build();
        HttpResponse<Void> response = client.send(request, HttpResponse.BodyHandlers.discarding());
        assertEquals(response.statusCode(), 200);
    } catch (IOException | InterruptedException ioe) {
        throw new RuntimeException(ioe);
    } finally {
        server.stop();
    }
}
 
源代码19 项目: Java-Coding-Problems   文件: Main.java
private static HttpResponse.PushPromiseHandler<String> pushPromiseHandler() {
    return (HttpRequest initiatingRequest, HttpRequest pushPromiseRequest,
            Function<HttpResponse.BodyHandler<String>, CompletableFuture<HttpResponse<String>>> acceptor) -> {
        CompletableFuture<Void> pushcf = acceptor.apply(HttpResponse.BodyHandlers.ofString())
                .thenApply(HttpResponse::body)
                .thenAccept((b) -> System.out.println("\nPushed resource body:\n " + b));

        asyncPushRequests.add(pushcf);

        System.out.println("\nJust got promise push number: " + asyncPushRequests.size());
        System.out.println("\nInitial push request: " + initiatingRequest.uri());
        System.out.println("Initial push headers: " + initiatingRequest.headers());
        System.out.println("Promise push request: " + pushPromiseRequest.uri());
        System.out.println("Promise push headers: " + pushPromiseRequest.headers());
    };
}
 
源代码20 项目: piranha   文件: HttpServerTest.java
/**
 * Test file.
 *
 * @throws Exception when a serious error occurs.
 */
@Test
public void testFile() throws Exception {
    HttpServer server = createServer(8765);
    server.start();
    try {
        HttpClient client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder(URI.create("http://localhost:8765/pom.xml")).build();
        HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
        assertEquals(response.statusCode(), 200);
        String responseText = response.body();
        assertTrue(responseText.contains("modelVersion"));
    } catch (IOException ioe) {
        throw new RuntimeException(ioe);
    } finally {
        server.stop();
    }
}
 
源代码21 项目: openapi-generator   文件: PetApi.java
private HttpRequest.Builder updatePetWithFormRequestBuilder(Long petId, String name, String status) throws ApiException {
  // verify the required parameter 'petId' is set
  if (petId == null) {
    throw new ApiException(400, "Missing the required parameter 'petId' when calling updatePetWithForm");
  }

  HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

  String localVarPath = "/pet/{petId}"
      .replace("{petId}", ApiClient.urlEncode(petId.toString()));

  localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

  localVarRequestBuilder.header("Accept", "application/json");

  localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.noBody());
  if (memberVarReadTimeout != null) {
    localVarRequestBuilder.timeout(memberVarReadTimeout);
  }
  if (memberVarInterceptor != null) {
    memberVarInterceptor.accept(localVarRequestBuilder);
  }
  return localVarRequestBuilder;
}
 
源代码22 项目: openapi-generator   文件: FakeApi.java
private HttpRequest.Builder fakeOuterStringSerializeRequestBuilder(String body) throws ApiException {

    HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

    String localVarPath = "/fake/outer/string";

    localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

    localVarRequestBuilder.header("Content-Type", "application/json");
    localVarRequestBuilder.header("Accept", "application/json");

    try {
      byte[] localVarPostBody = memberVarObjectMapper.writeValueAsBytes(body);
      localVarRequestBuilder.method("POST", HttpRequest.BodyPublishers.ofByteArray(localVarPostBody));
    } catch (IOException e) {
      throw new ApiException(e);
    }
    if (memberVarReadTimeout != null) {
      localVarRequestBuilder.timeout(memberVarReadTimeout);
    }
    if (memberVarInterceptor != null) {
      memberVarInterceptor.accept(localVarRequestBuilder);
    }
    return localVarRequestBuilder;
  }
 
private void pullFUTA404News() throws IOException, InterruptedException {
    HttpRequest request = HttpRequest.newBuilder()
            .uri(URI.create("http://www.futa404.com/category/dmaz")).GET().build();
    String body = httpClient.send(request, HttpResponse.BodyHandlers.ofString()).body();
    Document doc = Jsoup.parse(body);
    Elements elements = doc.getElementsByClass("card flex-fill mb-4 mb-sm-4-2 mb-md-4 mb-lg-4-2");
    List<ACGNew> acgNewList = elements.stream().map(e -> {
        String cover = e.getElementsByClass("lazyload custom-hover-img original").get(0).attr("src");
        Element t = e.getElementsByClass("custom-hover d-block").get(0);
        String title = t.attr("title");
        String rerfererUrl = t.attr("href");
        String time = e.getElementsByClass("u-time").get(0).text();
        LocalDate createDate;
        if (time.contains("-")) {
            createDate = LocalDate.parse(time);
        } else {
            createDate = LocalDate.now();
        }
        String intro = e.getElementsByClass("text-l2 font-md-12 text-secondary").get(0).text();
        return new ACGNew(title, intro, NewsCrawlerConstant.FUTA404, cover, rerfererUrl, createDate, NewsCrawlerConstant.FUTA404);
    }).collect(Collectors.toList());
    process(acgNewList, "class", "post-content suxing-popup-gallery");
}
 
源代码24 项目: java11-examples   文件: Example.java
public static void basicAuth() throws Exception {
    HttpClient client = HttpClient.newBuilder()
            .authenticator(new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication("username", "password".toCharArray());
                }
            })
            .build();

    HttpRequest request = HttpRequest.newBuilder()
            .uri(new URI("https://labs.consol.de"))
            .GET()
            .build();

    HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
    System.out.println(response.statusCode());
    System.out.println(response.body());
}
 
源代码25 项目: openapi-generator   文件: UserApi.java
private HttpRequest.Builder getUserByNameRequestBuilder(String username) throws ApiException {
  // verify the required parameter 'username' is set
  if (username == null) {
    throw new ApiException(400, "Missing the required parameter 'username' when calling getUserByName");
  }

  HttpRequest.Builder localVarRequestBuilder = HttpRequest.newBuilder();

  String localVarPath = "/user/{username}"
      .replace("{username}", ApiClient.urlEncode(username.toString()));

  localVarRequestBuilder.uri(URI.create(memberVarBaseUri + localVarPath));

  localVarRequestBuilder.header("Accept", "application/json");

  localVarRequestBuilder.method("GET", HttpRequest.BodyPublishers.noBody());
  if (memberVarReadTimeout != null) {
    localVarRequestBuilder.timeout(memberVarReadTimeout);
  }
  if (memberVarInterceptor != null) {
    memberVarInterceptor.accept(localVarRequestBuilder);
  }
  return localVarRequestBuilder;
}
 
源代码26 项目: hellokoding-courses   文件: HttpClientExamples.java
@Test
public void postJson() throws IOException, InterruptedException {
    HttpClient client = HttpClient.newHttpClient();

    Book book = new Book(1, "Java HttpClient in practice");
    String body = objectMapper.writeValueAsString(book);

    HttpRequest request = HttpRequest.newBuilder()
        .uri(URI.create("http://localhost:8081/test/resource"))
        .header("Accept", "application/json")
        .header("Content-Type", "application/json")
        .POST(HttpRequest.BodyPublishers.ofString(body))
        .build();

    HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

    assertThat(response.statusCode()).isEqualTo(200);
    assertThat(objectMapper.readValue(response.body(), Book.class).id).isEqualTo(1);
}
 
源代码27 项目: Fibry   文件: HttpChannel.java
public HttpChannel(String url, HttpMethod method, Executor executor, Consumer<HttpRequest> requestCustomizer, boolean encodeBase64) {
    this.url = url;
    this.method = method;
    this.executor = executor;
    this.encodeBase64 = encodeBase64;
    this.requestCustomizer = requestCustomizer;
}
 
@Cacheable(value = "searchSuggestions")
public CompletableFuture<List<SearchSuggestion>> getSearchSuggestionFromMoeGirl(@SensitiveCheck String keyword) {
    HttpRequest httpRequest = HttpRequest.newBuilder()
            .GET()
            .uri(URI.create("https://zh.moegirl.org/index.php?limit=2&search=" + URLEncoder.encode(keyword, StandardCharsets.UTF_8)))
            .build();
    //正则提取关键词
    return httpClient.sendAsync(httpRequest, HttpResponse.BodyHandlers.ofString()).thenApply(r ->
            moeGirlPattern.matcher(r.body()).results().map(result -> {
                String matchKeyword = result.group();
                return new SearchSuggestion(matchKeyword, translatedByYouDao(keyword));
            }).collect(Collectors.toList())
    );

}
 
源代码29 项目: keycloak-extension-playground   文件: Main.java
public static HttpRequest.BodyPublisher ofFormData(Map<Object, Object> data) {
    var builder = new StringBuilder();
    for (Map.Entry<Object, Object> entry : data.entrySet()) {
        if (builder.length() > 0) {
            builder.append("&");
        }
        builder.append(URLEncoder.encode(entry.getKey().toString(), StandardCharsets.UTF_8));
        builder.append("=");
        builder.append(URLEncoder.encode(entry.getValue().toString(), StandardCharsets.UTF_8));
    }
    return HttpRequest.BodyPublishers.ofString(builder.toString());
}
 
private void dealRelationWithIllustration(List<Spotlight> spotlights) {
    spotlights.stream().parallel().forEach(s -> {
        HttpRequest.Builder uri = HttpRequest.newBuilder()
                .uri(URI.create(s.getArticleUrl()));
        RequestUtil.decorateHeader(uri);
        HttpRequest getArticle = uri.GET().build();
        try {
            String body = httpClient.send(getArticle, HttpResponse.BodyHandlers.ofString()).body();
            Optional<MatchResult> first = imageUrlPattern.matcher(body).results().findFirst();
            first.ifPresent(matchResult -> s.setThumbnail(matchResult.group()));
            List<Integer> illustIds = illustIdPattern.matcher(body).results().map(m -> Integer.parseInt(m.group())).collect(Collectors.toList());
            //联系入库
            if (illustIds.size() > 0) {
                spotlightMapper.insertRelation(s.getId(), illustIds);
                //查找出没在数据库的画作
                illustIds = illustrationService.queryIllustsNotInDb(illustIds);
                //拉取
                if (illustIds.size() > 0) {
                    List<Illustration> illustrations = illustIds.stream().map(illustrationService::pullIllustrationInfo).filter(Objects::nonNull).collect(Collectors.toList());
                    //持久化
                    if (illustrations.size() > 0) {
                        illustrationService.saveToDb(illustrations);
                    }
                }
            }
        } catch (IOException | InterruptedException e) {
            System.out.println("抓取spot错误" + e.getMessage());
        }
    });
    saveToDb(spotlights);
    System.out.println("spotlight处理完毕");
}
 
源代码评论
动弹
沙发等你来抢
 类所在包
 同包方法