Apache HTTP客户端,来自特定网络接口的请求

Jho*_*hon 5 java apache apache-httpclient-4.x

我有4个互联网IP的机器,我想知道我是否可以使apache http客户端从特定的IP /网络接口发出请求

ok2*_*k2c 5

使用HttpClient 4.3 API

    RequestConfig config = RequestConfig.custom()
            .setLocalAddress(InetAddress.getByAddress(new byte[] {127,0,0,1}))
            .build();
    HttpGet httpGet = new HttpGet("/stuff");
    httpGet.setConfig(config);
    CloseableHttpClient httpClient = HttpClients.createDefault();
    try {
        CloseableHttpResponse response = httpClient.execute(httpGet);
        try {
            // do something useful
        } finally {
            response.close();
        }
    } finally {
        httpClient.close();
    }
Run Code Online (Sandbox Code Playgroud)


Pet*_*Mmm 0

从来没有这样做过,但 API 中有一个ClientConnectionOperator接口(还有一些工厂)来创建套接字。也许您可以实现自己的并使用具体接口创建套接字。