Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache HTTP client, request from specific network interface

I have machine with 4 internet IP's and I want to know if I can make apache http client to make requests from specific ip/network interface

like image 578
Jhon Avatar asked Sep 07 '13 11:09

Jhon


1 Answers

Using HttpClient 4.3 APIs

    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();
    }
like image 162
ok2c Avatar answered Oct 20 '22 14:10

ok2c