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 Simone,

Many thanks for your quick help.
I plan to change code as below. Could you please help check if it is correct?

...
             HttpClient client = new HttpClient(clientTLSFactory);
            client.setBindAddress(new InetSocketAddress("10.9.101.100", 0)); //local IP

            client.setSocketAddressResolver(new SocketAddressResolver() {
            	  @Override
            	  public void resolve(String host, int port, Promise<List<InetSocketAddress>> promise) {
                      try
                      {
                          //above host is the FQDN of remote https server after SRV record resolving,
                          //in this example, it is "www.eclipse.org"
                          //DO DNS A/AAAA lookup for above host to get a list of IPs
                          //select one IP from above IPs list according to some rule, 
                          //    and assign this IP to host
                          host = <the selected IP>; //e.g, 198.41.30.198
		
                          InetAddress[] addresses = InetAddress.getAllByName(host);

                          List<InetSocketAddress> result = new ArrayList<>(addresses.length);
                          for (InetAddress address : addresses)
                              result.add(new InetSocketAddress(address, port));

                          if (result.isEmpty())
                              promise.failed(new UnknownHostException());
                          else
                              promise.succeeded(result);
                      }
                      catch (Throwable x)
                      {
                          promise.failed(x);
                      }
                  }
              });
            
            Request httpsreq = client.newRequest("https://www.eclipse.org/jetty/";)
                                          .method(org.eclipse.jetty.http.HttpMethod.POST);

            httpsreq.header(headerName, headerValue); 
            httpsreq.content(new BytesContentProvider("Hello Jetty"), "application/json");
            httpsreq.send(new Response.Listener.Adapter() { ......});


Regards,
William


-----Original Message-----
From: jetty-users-bounces@xxxxxxxxxxx <jetty-users-bounces@xxxxxxxxxxx> On Behalf Of Simone Bordet
Sent: 2020年2月21日 17:00
To: JETTY user mailing list <jetty-users@xxxxxxxxxxx>
Subject: 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.
_______________________________________________
jetty-users mailing list
jetty-users@xxxxxxxxxxx
To change your delivery options, retrieve your password, or unsubscribe from this list, visit https://www.eclipse.org/mailman/listinfo/jetty-users

Back to the top