You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Wolfram Alpha <wo...@googlemail.com> on 2012/07/20 17:06:35 UTC
[HttpClient 4] local socket port
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
Re: [HttpClient 4] local socket port
Posted by Wolfram Alpha <wo...@googlemail.com>.
Thank you for your solutions! Both are working as expected and I have
already implemented the solution with the ClientConnectionOperator.
Re: [HttpClient 4] local socket port
Posted by Oleg Kalnichevski <ol...@apache.org>.
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
> <wo...@googlemail.com> 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: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>
---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org
Re: [HttpClient 4] local socket port
Posted by "Telvis Calhoun Jr." <te...@gmail.com>.
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
On Fri, Jul 20, 2012 at 11:06 AM, Wolfram Alpha
<wo...@googlemail.com> 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: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org