错误:(226、34)java:找不到符号符号:变量 BodyPublisher 位置:类 jdk.incubator.http.HttpRequest
private static JSONObject sendRequest(JSONObject json) throws Exception {
HttpClient client;
InetSocketAddress proxy = getRandomProxy();
if(proxy == null) {
client = HttpClient.newHttpClient();
} else {
client = HttpClient.newBuilder().proxy(ProxySelector.of(proxy)).build();
}
HttpRequest httpRequest = HttpRequest
.newBuilder(new URI(BASE_URL))
.header("Accept", "application/json")
.header("Content-Type", "application/json")
.timeout(TIMEOUT_DURATION)
.POST(HttpRequest.BodyPublisher.fromString(json.toString()))
.build();
HttpResponse<String> httpResponse = client.send(httpRequest, HttpResponse.BodyHandler.asString());
String jsonResponse = httpResponse.body();
return new JSONObject(jsonResponse);
}
模块信息.java:
module MyProject {
requires jdk.incubator.httpclient;
}
我已经在 Linux 上使用 OpenJDK 10 和 Oracle JDK 10 进行了尝试。