Dear Oleg, It works.
Thank you very much!! Sincerely, Micky -----Original Message----- From: Oleg Kalnichevski [mailto:[email protected]] Sent: Saturday, September 19, 2009 12:43 AM To: HttpClient User Discussion Subject: Re: How to get local address after successful http connection On Thu, 2009-09-17 at 14:38 +0800, Micky wrote: > Dear all, > > > > After a successful http connection, I want to get the local address with the > target connection. > > But I don’t know how to get it. Any suggestion? > > > > I know there is HttpRoute will be determined when a HttpClient.execute(), and > HttpRoute.getLocalAddress() may be useful. > > But there is no way to get it (HttpRoute) through the public interface > according to my experience and my survey of the API javadoc. > > > > Because I have two network cards and their subnets are different. > > One is 192.168.10.x, another is 192.168.20.x > > > > I want to automatically know if I connect to 192.168.30.x, which network card > IP will be used? > > I think I may get the local address after a successful connection. Am I right? > > > > I use HttpClient 4.0. > > > > Thanks in advance. > The best way to get access to the underlying HTTP connection is by using a protocol interceptor. Request interceptors will get called after the connection has been established but before the request gets executed. Response interceptors will get called immediately after a response head has bas been received. Choose whichever plugin point is more appropriate for your application. Here's an example of getting HTTP connection socket details using a request inerceptor: ------ DefaultHttpClient httpclient = new DefaultHttpClient(); httpclient.addRequestInterceptor(new HttpRequestInterceptor() { public void process( final HttpRequest request, final HttpContext context) throws HttpException, IOException { HttpInetConnection conn = (HttpInetConnection) context .getAttribute(ExecutionContext.HTTP_CONNECTION); System.out.println("local address: " + conn.getLocalAddress()); System.out.println("local port: " + conn.getLocalPort()); } }); HttpHost target = new HttpHost("localhost", 8080, "http"); HttpGet req = new HttpGet("/"); System.out.println("executing request to " + target); HttpResponse rsp = httpclient.execute(target, req); HttpEntity entity = rsp.getEntity(); System.out.println("----------------------------------------"); System.out.println(rsp.getStatusLine()); System.out.println("----------------------------------------"); if (entity != null) { entity.consumeContent(); } ------ For details see: http://hc.apache.org/httpcomponents-client/tutorial/html/ Hope this helps Oleg > > > > > Sincerely, > > Micky > --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
