Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [jetty-users] how does org.eclipse.jetty.client.HttpClient or org.eclipse.jetty.client.api.Request set the IP for the socket connect

Hi,

On Fri, Feb 21, 2020 at 8:52 AM Cao, William (NSB - CN/Qingdao)
<william.cao@xxxxxxxxxxxxxxx> wrote:
>
> Hi,
>
> I am using below codes to write a https client using Jetty 9.4.7.
>
> I use the FQDN kind of url https://www.eclipse.org/jetty/ to do newRequest, I checked the jetty code, and got that Jetty will use this url to create socket connection.
>
> We often have a requirement that the application need balance the IP resolved from FQDN and record the IP which can’t successfully be connected, and not depend on DNS server to do it. i.e., our application will do DNS/SRV lookup on www.eclipse.org in below example, there will be several IPs retrieved, our application will select one IP (e.g., 198.41.30.198 ) according to some rule, and then use this IP to build the socket, and the request still use FQDN kind of url https://www.eclipse.org/jetty/ as the request url, use FQDN www.eclipse.org:443 as the Host header.
>
> I can’t find the way to specify the remote InetSocketAddress with IP so as to build socket connection, could you please help with that?
>
> HttpClient client = new HttpClient(clientTLSFactory);
> InetSocketAddress  localAddress = new InetSocketAddress("10.9.101.100", 0); //local host IP
> client.setBindAddress(localAddress);
>
> Request httpsreq httpsreq = client.newRequest("https://www.eclipse.org/jetty/";)
>                     .method(org.eclipse.jetty.http.HttpMethod.POST);
>
> //Here I want to specify the IP for this request, to build the socket connection.
> //e.g., //httpsreq.setRemoteInetAddress(“198.41.30.198”);
>
> httpsreq.header(“Host”, “www.eclipse.org:443”);
> httpsreq.header(otherheaderName, headerValue);
> httpsreq.content(new BytesContentProvider("Hello Jetty"), "application/json");
>
> httpsreq.send(new Response.Listener.Adapter() {
> ......
>     @Override
>     public void onFailure(Response response, Throwable failure) {
>         ...
>     }
>
>     @Override
>     public void onSuccess(Response response) {
>         ...
>     }
> ......
> }

You want to use a custom SocketAddressResolver, so:

client.setSocketAddressResolver(new SocketAddressResolver() {
  @Override
  public void resolve(String host, int port,
Promise<List<InetSocketAddress>> promise) {
    // Do your logic here.
  }
}

Let us know if this worked for you.

-- 
Simone Bordet
----
http://cometd.org
http://webtide.com
Developer advice, training, services and support
from the Jetty & CometD experts.


Back to the top