get 同步和异步请求
<code>#同步
public void testGet() throws IOException, InterruptedException {
HttpClient client = HttpClient.newHttpClient();
HttpRequest request = HttpRequest.newBuilder()
.uri(URI.create("https://www.toutiao.com"))
.build();
HttpResponse<string> 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<string> result = client.sendAsync(request, HttpResponse.BodyHandlers.ofString())
.thenApply(HttpResponse::body);
System.out.println(result.get());
}/<string>/<string>/<code>
post
<code>#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<stockdto> 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提
/<stockdto>/<code>
Authentication
<code>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<string> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
// print status code
System.out.println(response.statusCode());
// print response body
System.out.println(response.body());
}
}
/<string>/<code>
redirect
<code>\tprivate final HttpClient httpClient = HttpClient.newBuilder()
.followRedirects(HttpClient.Redirect.NEVER)
.build();/<code>
Timeout
<code>\tprivate final HttpClient httpClient = HttpClient.newBuilder()
.connectTimeout(Duration.ofSeconds(5))
.build();/<code>
Proxy
<code>\tprivate final HttpClient httpClient = HttpClient.newBuilder()
.proxy(ProxySelector.of(new InetSocketAddress("your-company-proxy.com", 8080)))
.build();/<code>
閱讀更多 達升笑聊IT 的文章