0

This works.

HttpHost proxy = new HttpHost(properties.getProxyHost(), properties.getProxyPort());
RequestConfig config = RequestConfig.custom()
  .setProxy(proxy)
  .build();

URI uri = new URIBuilder(properties.getUri())
  .build();

HttpGet request = new HttpGet(uri);
request.setConfig(config);

HttpResponse response = client.execute(request);

But when I try to set the proxy at startup

java \
${JAVA_OPTS} \
-Dhttp.proxyHost=my-proxy \
-Dhttp.proxyPort=my-port \
-jar ${PROJECT_DIR}/${TARGET_JAR} \

And run this script

URI uri = new URIBuilder(properties.getUri())
  .build();

HttpGet request = new HttpGet(uri);

HttpResponse response = client.execute(request);

It fails with this error

I/O exception (java.net.SocketException) caught when processing request to {s}->https://site-address:443: Network is unreachable (connect failed)

Why does it fail? Isn't this basicaly the same thing?

Edit

My dependency

<dependency>
  <groupId>org.apache.httpcomponents</groupId>
  <artifactId>httpclient</artifactId>
  <version>4.5.13</version>
</dependency>
gjin
  • 860
  • 1
  • 14
  • 28
  • I can't see why that would be the same thing, you didn't say how `properties` variable is set or its type. – Andrés Alcarraz Mar 16 '23 at 15:17
  • 1
    What library are you using? What does its documentation say about configuring a proxy? Why do you assume that passing those properties will magically make the library use them? – Jorn Mar 16 '23 at 15:32
  • @Jorn because https://stackoverflow.com/questions/120797/how-do-i-set-the-proxy-to-be-used-by-the-jvm and https://docs.oracle.com/javase/7/docs/api/java/net/doc-files/net-properties.html – gjin Mar 16 '23 at 16:14
  • @And It's a pojo. A few vars and their getters and setters. Used to pass host and port values. – gjin Mar 16 '23 at 16:19
  • 1
    That oracle documentation is for the jvm not the `HttpHost` class. Jsut from the start is states that: `There are a few standard system properties used to alter the mechanisms and behavior of the various classes of the java.net package` – Andrés Alcarraz Mar 16 '23 at 16:25

1 Answers1

0

RequestConfig.Builder is a low-level API, it does NOT resolve proxy automatically.

To make your code work the same way as HttpClient you need to choose and configure proxy manually:

public static void main(String[] args) {
    URI uri = URI.create("https://www.google.com");
    RequestConfig config = RequestConfig.custom()
        .setProxy(resolveProxy(uri))
        .build();
    ...
}

static HttpHost resolveProxy(URI uri) {
    return ProxySelector.getDefault().select(uri).stream().map(it -> {
        if (it.type() != Proxy.Type.HTTP) {
            return null;
        }
        if (!(it.address() instanceof InetSocketAddress)) {
            return null;
        }
        InetSocketAddress address = (InetSocketAddress) it.address();
        return new HttpHost(
            address.isUnresolved() ? address.getHostName() : address.getAddress().getHostAddress(),
            address.getPort()
        );
    }).filter(Objects::nonNull).findFirst().orElse(null);
}
ursa
  • 4,404
  • 1
  • 24
  • 38