On Fri, 2012-07-20 at 14:41 -0400, Telvis Calhoun Jr. wrote:
> Your problem seems similar to mine. Oleg recommended extending the
> DefaultClientConnectionOperator class to add the socket attribute to
> the context. Something like:
> 
> class MyClientConnectionOperator extends DefaultClientConnectionOperator {
> 
>     public MyClientConnectionOperator(SchemeRegistry schemes) {
>         super(schemes);
>     }
> 
>     @Override
>     protected void prepareSocket(
>             final Socket sock,
>             final HttpContext context,
>             final HttpParams params) throws IOException {
>         super.prepareSocket(sock, context, params);
>         context.setAttribute("sock-port", sock.getLocalPort());
>     }
> 
> };
> 
> // thread
> http://old.nabble.com/get-InetAddress-for-the-HTTP_TARGET_HOST-tt34157287.html
> 
> 

Custom ClientConnectionOperator is still the way to go if one wants to
exert full control over the process of connection initialization.
However, I just figured there is probably a much simpler way to get the
endpoint details of an active connection

---
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.addRequestInterceptor(new HttpRequestInterceptor() {
    
    public void process(
            HttpRequest request, HttpContext context) throws
HttpException, IOException {
        HttpInetConnection conn = (HttpInetConnection)
context.getAttribute(
                ExecutionContext.HTTP_CONNECTION);
        System.out.println(conn.getLocalAddress() + ":" +
conn.getLocalPort());
        System.out.println(conn.getRemoteAddress() + ":" +
conn.getRemotePort());
    }
});
---

Hope this helps

Oleg





> On Fri, Jul 20, 2012 at 11:06 AM, Wolfram Alpha
> <[email protected]> wrote:
> > Hi
> >
> > I'm using the Apache HttpClient 4.1.2 and I want to get the local port of
> > the underlying socket. I have not found a reasonable way to get the
> > port-number without changing a lot of the httpclient code, which I don't
> > want to do.
> >
> > Is there realy no easy way to get the port-number?
> >
> > Any suggestions are appreciated!
> >
> > Thanks
> 
> ---------------------------------------------------------------------
> 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]

Reply via email to