HttpHost uses -1 to mean “no explicit port in the URI”. That’s valid for
http:// and https:// targets, but HttpRoute needs a concrete port because
it represents the fully resolved end-point used to open a socket. The
default route planners resolve the scheme’s default port before
constructing the route.  In a custom RoutePlanner, just normalize the host
first

Alternatively, consider subclassing DefaultRoutePlanner (or using
SystemDefaultRoutePlanner with a custom ProxySelector) so you can keep the
built-in normalization and only override your proxy decision.

Arturo


On Wed, Nov 5, 2025 at 12:51 PM Jonathan Ho <[email protected]>
wrote:

> Hi,
> I have
> HttpClientBuilder httpclientB = HttpClients.custom()
>             .setRoutePlanner(new CustomRoutePlanner(p));
>
> where in CustomRoutePlanner's determineRoute I test the HttpHost target to
> see if it needs to go through a proxy or not.
> If the HttpHost target is say https://www.test.com and it doesn't go
> through the proxy
>
> I return new HttpRoute(target) and this throws an exception of port value
> of -1
>
> the line of code is HttpRoute.java
> Args.notNegative(targetHost.getPort(), "Target port");
>
> it seems HttpHost is default port to -1 for default schema port
>
> Is this a bug or am I doing something wrong??
>
> For now I added this workaround
> if (target.getPort() == -1) {
>                 int port = target.getSchemeName().equals("http") ? 80 :
> 443;
>                 target = new HttpHost(target.getSchemeName(),
> target.getHostName(), port);
>
> System.out.println("====determineRoute="+target.toString());
>                 System.out.println("====determineRoute="+target.getPort());
>  }
>  return new HttpRoute(target);
>
> Thanks
>

Reply via email to