使用httpclient 4.5x获取请求地址的ip ( target IP)

当域名绑定多个ip的时候,需要指定当前请求的域名是哪个ip地址。
HttpClient 4.x how to find target IP of the requested site
直接通过httpclient 去获取,查到的资料基本基于3.x版本的扩展。
最后搜索到:
https://stackoverflow.com/questions/23281462/apache-httpclient-4-3-3-how-to-find-target-ip-of-the-requested-site
解决方式可以参考:

Since the Socket s = super.createSocket(context); creates simple java Socket in the abstraction, I tried to override connectSocket method and it works this time. Here is the working example:
public static void main(String[] args) throws ClientProtocolException,
            IOException {

        String s = "http://google.com";

        PoolingHttpClientConnectionManager m = new PoolingHttpClientConnectionManager(
                RegistryBuilder.<ConnectionSocketFactory> create()
                        .register("http", new PlainConnectionSocketFactory() {

                            @Override
                            public Socket connectSocket(int connectTimeout,
                                    Socket socket, HttpHost host,
                                    InetSocketAddress remoteAddress,
                                    InetSocketAddress localAddress,
                                    HttpContext context) throws IOException {
                                context.setAttribute("sock-address",
                                        remoteAddress);
                                return super.connectSocket(connectTimeout,
                                        socket, host, remoteAddress,
                                        localAddress, context);
                            }
                        }).build(), new SystemDefaultDnsResolver());

        CloseableHttpClient minimal = HttpClients.custom()
                .setConnectionManager(m).build();

        HttpGet get = new HttpGet(s);

        HttpClientContext context = HttpClientContext.create();

        CloseableHttpResponse response = minimal.execute(get, context);

        InetSocketAddress addr = (InetSocketAddress) context
                .getAttribute("sock-address");

        System.out.println(addr.getAddress().getHostAddress());

    }

对于4.x版本,也可以通过上下文的方式获取到当前链接,由于连接在返回时候可能就会被释放。因此通过扩展HttpRequestExecutor的方式去提前获取到ip并保持在context,之后通过context获取ip即可:

一、增加HttpRequestExecutorEx获取ip

public class HttpRequestExecutorEx extends HttpRequestExecutor {

    public static final String HTTP_HOSTADDRESS  = "http.hostAddress";

    @Override
    public HttpResponse execute(
            final HttpRequest request,
            final HttpClientConnection conn,
            final HttpContext context) throws IOException, HttpException {
        Args.notNull(request, "HTTP request");
        Args.notNull(conn, "Client connection");
        Args.notNull(context, "HTTP context");
        try {
            HttpResponse response = doSendRequest(request, conn, context);
            if (response == null) {
                response = doReceiveResponse(request, conn, context);
            }
            processRequestIp(conn, context);
            return response;
        } catch (final IOException ex) {
            processRequestIp(conn,context);
            closeConnection(conn);
            throw ex;
        } catch (final HttpException ex) {
            processRequestIp(conn, context);
            closeConnection(conn);
            throw ex;
        } catch (final RuntimeException ex) {
            processRequestIp(conn, context);
            closeConnection(conn);
            throw ex;
        }
    }

    private void processRequestIp(HttpClientConnection conn, HttpContext context) {
        if (conn instanceof ManagedHttpClientConnection) {
            ManagedHttpClientConnection managedConn = (ManagedHttpClientConnection) conn;
            if (managedConn != null && managedConn.isOpen()) {
                Socket socket = managedConn.getSocket();
                String hostAddress = socket.getInetAddress().getHostAddress();
                context.setAttribute(HTTP_HOSTADDRESS, hostAddress);
            }
        }
    }


    private static void closeConnection(final HttpClientConnection conn) {
        try {
            conn.close();
        } catch (final IOException ignore) {
        }
    }

}
 

二、将该类增加到httpclient中:

        CloseableHttpClient httpClient = HttpClients.custom()
                .setRequestExecutor(new HttpRequestExecutorEx())
                .build();

三、 最后从context获取

BasicHttpContext basicHttpContext = new BasicHttpContext();
HttpResponse response = httpClient.execute(post, basicHttpContext);
hostAddress = (String) basicHttpContext.getAttribute(HttpRequestExecutorEx.HTTP_HOSTADDRESS);
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值