尝试使用java.net.http.HttpClient. 它无需代理使用即可工作。代理有效且有效。代理支持 https/s 和 socks5 连接。但是,当我尝试使用示例中的代理时,收到此错误:
 java.io.IOException: HTTP/1.1 header parser received no bytes
我认为我正确地将代理传递给客户端(例如https://openjdk.java.net/groups/net/httpclient/recipes.html),尽管我传递的是代理 IP,而不是域,这似乎是被 接受InetSocketAddress。
例子:
                HttpRequest request = HttpRequest.newBuilder()
                        .uri(new URI("http://google.com"))
                        .headers("Content-Type", "application/json;charset=UTF-8")
                        .GET()
                        .build();
                HttpResponse<String> response = HttpClient
                        .newBuilder()
                        .connectTimeout(Duration.ofSeconds(15))
                        .proxy(ProxySelector.of(
                            new InetSocketAddress(
                                    "1.1.1.1", 1111
                            )
                        ))
                        .build()
                        .send(request, HttpResponse.BodyHandlers.ofString());
java ×1