asp.net httpclient post 请求头_Java11的HttpClient的使用
生活随笔
收集整理的這篇文章主要介紹了
asp.net httpclient post 请求头_Java11的HttpClient的使用
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
get 同步和異步請求
#同步 public void testGet() throws IOException, InterruptedException { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://www.toutiao.com")) .build(); HttpResponse response = client.send(request, HttpResponse.BodyHandlers.ofString()); }#異步 public void testAsyncGet() throws ExecutionException, InterruptedException { HttpClient client = HttpClient.newHttpClient(); HttpRequest request = HttpRequest.newBuilder() .uri(URI.create("https://www.baidu.com")) .build(); CompletableFuture result = client.sendAsync(request, HttpResponse.BodyHandlers.ofString()) .thenApply(HttpResponse::body); System.out.println(result.get()); }post
#json提交 public void testPostJsonGetJson() throws ExecutionException, InterruptedException, JsonProcessingException { ObjectMapper objectMapper = new ObjectMapper(); StockDto dto = new StockDto(); dto.setName("hj"); dto.setSymbol("hj"); dto.setType(StockDto.StockType.SH); String requestBody = objectMapper .writerWithDefaultPrettyPrinter() .writeValueAsString(dto); HttpRequest request = HttpRequest.newBuilder(URI.create("http://toutiao/test/")) .header("Content-Type", "application/json") .POST(HttpRequest.BodyPublishers.ofString(requestBody)) .build(); CompletableFuture result = HttpClient.newHttpClient() .sendAsync(request, HttpResponse.BodyHandlers.ofString()) .thenApply(HttpResponse::body) .thenApply(body -> { try { return objectMapper.readValue(body,StockDto.class); } catch (IOException e) { return new StockDto(); } }); }#form提Authentication
package com.mkyong.http;import java.io.IOException;import java.net.Authenticator;import java.net.PasswordAuthentication;import java.net.URI;import java.net.http.HttpClient;import java.net.http.HttpRequest;import java.net.http.HttpResponse;public class Java11HttpClientExample6 { private final HttpClient httpClient = HttpClient.newBuilder() .authenticator(new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication( "user", "password".toCharArray()); } }) .build(); public static void main(String[] args) throws IOException, InterruptedException { Java11HttpClientExample6 obj = new Java11HttpClientExample6(); obj.sendGET(); } private void sendGET() throws IOException, InterruptedException { HttpRequest request = HttpRequest.newBuilder() .GET() .uri(URI.create("http://localhost:8080/books")) .setHeader("User-Agent", "Java 11 HttpClient Bot") // add request header .build(); HttpResponse response = httpClient.send(request, HttpResponse.BodyHandlers.ofString()); // print status code System.out.println(response.statusCode()); // print response body System.out.println(response.body()); }}redirect
private final HttpClient httpClient = HttpClient.newBuilder() .followRedirects(HttpClient.Redirect.NEVER) .build();Timeout
private final HttpClient httpClient = HttpClient.newBuilder() .connectTimeout(Duration.ofSeconds(5)) .build();Proxy
private final HttpClient httpClient = HttpClient.newBuilder() .proxy(ProxySelector.of(new InetSocketAddress("your-company-proxy.com", 8080))) .build();總結(jié)
以上是生活随笔為你收集整理的asp.net httpclient post 请求头_Java11的HttpClient的使用的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: blp模型 上读下写_Java高并发编程
- 下一篇: 甘特图 知乎_安利!拥有这5款甘特图工具