在 Java 中请求 HTTP 客户端并获取响应
我们将使用 Java 中的 HTTP 客户端来发送请求和接收响应。同时,你还将学习如何使用主体处理程序、构建器和其他底层方法来发送 HTTP 客户端请求。
在 Java 中发送 HTTP 请求并从客户端接收 JSON 响应
我们将使用程序员用来评估其 HTTP 请求的演示 JSON 网站。这里是 https://blog.typicode.com/
。
在此之前,请记下以下对你有帮助的方法。
-
HttpClient send2client = HttpClient.newHttpClient();
– 我们使用它向客户端发送 http 请求并接收响应。 -
HttpRequest Req2client = HttpRequest();
– 此方法有助于将 http 请求构建为具有以下参数的实例。
2.1.newBuilder();
– 生成一个 Http 请求构建器。此方法返回一个构建标准 HTTP 客户端 API 对象的构建器。
2.2.uri("客户端 URL");
– 设置 http 请求的 URL。
2.3.build();
– 该参数构建并返回 http 请求。 -
HttpResponse<String> clientRes = send2client.send(Req2client, HttpResponse.BodyHandlers.ofString());
– 响应状态码、响应头、响应体,以及该响应对应的 HTTP Request 都可以通过这个类访问。
查看以下程序,它执行我们到目前为止讨论的所有内容。
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class Example1 {
public static void main(String[] args) throws IOException, InterruptedException {
HttpClient send2client = HttpClient.newHttpClient();
HttpRequest Req2client = HttpRequest.newBuilder().uri(URI.create("https://blog.typicode.com/")).build();
String format = System.getProperty("line.separator");
HttpResponse<String> clientRes = send2client.send(Req2client, HttpResponse.BodyHandlers.ofString());
System.out.println(" Requested Responses from the client" + format + "1: Status code" + format+ clientRes.statusCode() + format);
System.out.println("2: Uniform Resource Locator (URL) from the client" + clientRes.uri() + format);
System.out.println("3: SSL Session" + format + clientRes.sslSession() + format);
System.out.println("4: HTTP version" + format + clientRes.version() + format);
//System.out.println("5: Response Header" + format + clientRes.headers() + format);
//System.out.println("6: Response Body" + format + clientRes.body() + format);
}
}
输出:
Requested Responses from the client
1: Status code
200
2: Uniform Resource Locator (URL) from the client: https://blog.typicode.com/
3: SSL Session
Optional[jdk.internal.net.http.common.ImmutableExtendedSSLSession@5bcea91b]
4: HTTP version
HTTP_2
注意
你还可以了解更多关于在 Java 中实现 HTTP Post。
在 Java 中异步执行 HTTP 请求并获取响应
我们将在以下代码块中使用相同的 HttpRequest
方法,但具有以下功能。
sendAsync()
– 此客户端使用指定的响应正文处理程序异步发送指定的请求。
sendAsync()
和 HttpRequest
是发送和检索方法。两者对于 HTTP Web 处理程序都是安全的。
语法:
Cli.sendAsync(RQI, BodyHandlers.ofString())
.thenApply(HttpResponse::body) //optional
.thenAccept(System.out::println) //The action to take before completing the retrieved completion stage
.join(); //returts the response value
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
public class Example2 {
public static void main(String[] args) {
HttpClient Cli = HttpClient.newHttpClient();
HttpRequest RQI = HttpRequest.newBuilder()
.uri(URI.create("https://www.delftstack.com"))
.build();
Cli.sendAsync(RQI, BodyHandlers.ofString())
.thenApply(HttpResponse::body)
.thenAccept(System.out::println)
.join();
}
}
输出:
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布,任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站。本站所有源码与软件均为原作者提供,仅供学习和研究使用。如您对本站的相关版权有任何异议,或者认为侵犯了您的合法权益,请及时通知我们处理。