要给java.net.http.HttpClient的GET请求添加参数,可以通过以下步骤实现:
URI uri = URI.create("http://example.com/path?key=value");
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
.uri(uri);
requestBuilder.GET();
HttpRequest request = requestBuilder.build();
HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
完整的代码示例如下:
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
public class HttpClientExample {
public static void main(String[] args) throws Exception {
URI uri = URI.create("http://example.com/path?key=value");
HttpRequest.Builder requestBuilder = HttpRequest.newBuilder()
.uri(uri)
.GET();
HttpRequest request = requestBuilder.build();
HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());
System.out.println(response.body());
}
}
这样就可以给java.net.http.HttpClient的GET请求添加参数了。
领取专属 10元无门槛券
手把手带您无忧上云