2012-04-24 15 views
5

Tôi không thể giúp bản thân mình bằng cách trả lời câu hỏi này.đặt nonProxyHosts trong Apache HttpClient 4.1.3

Làm cách nào tôi có thể đặt nonProxyHosts trong Apache HttpClient 4.1.3?

Trong Httpclient 3.x cũ khá đơn giản. Bạn có thể thiết lập nó bằng các phương thức setNonProxyHosts.

Nhưng hiện tại, không có phương pháp tương đương cho phiên bản mới. Tôi đã được tìm kiếm trough các tài liệu api, hướng dẫn và ví dụ và havent tìm thấy giải pháp cho đến nay.

để thiết lập một proxy bình thường u chỉ có thể làm điều đó bằng cách này:

HttpHost proxy = new HttpHost("127.0.0.1", 8080, "http"); 
    DefaultHttpClient httpclient = new DefaultHttpClient(); 
    httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy); 

Có ai biết nếu có một trong số các giải pháp hộp trong phiên bản mới 4.1.3 httpclient lập nonProxyHosts hay làm Tôi phải tự mình làm như vậy

if (targetHost.equals(nonProxyHost) { 
    dont use a proxy 
    } 

Xin cảm ơn trước.

+0

tôi có thể giải quyết vấn đề này bằng cách sử dụng bộ chọn lọc. – Jools

+0

bạn có thể vui lòng cam kết giải pháp của bạn, tôi chạy vào cùng một vấn đề. – moohkooh

Trả lời

3

@moohkooh: đây là cách tôi giải quyết vấn đề.

DefaultHttpClient client = new DefaultHttpClient(); 

//use same proxy as set in the system properties by setting up a routeplan 
ProxySelectorRoutePlanner routePlanner = new ProxySelectorRoutePlanner(client.getConnectionManager().getSchemeRegistry(), 
    new LinkCheckerProxySelector()); 
client.setRoutePlanner(routePlanner); 

Và sau đó LinkcheckerProxySelector() của bạn sẽ giống như thế.

private class LinkCheckerProxySelector extends ProxySelector { 

@Override 
public List<Proxy> select(final URI uri) { 

    List<Proxy> proxyList = new ArrayList<Proxy>(); 

    InetAddress addr = null; 
    try { 
    addr = InetAddress.getByName(uri.getHost()); 
    } catch (UnknownHostException e) { 
    throw new HostNotFoundWrappedException(e); 
    } 
    byte[] ipAddr = addr.getAddress(); 

    // Convert to dot representation 
    String ipAddrStr = ""; 
    for (int i = 0; i < ipAddr.length; i++) { 
    if (i > 0) { 
     ipAddrStr += "."; 
    } 
    ipAddrStr += ipAddr[i] & 0xFF; 
    } 

//only select a proxy, if URI starts with 10.* 
    if (!ipAddrStr.startsWith("10.")) { 
    return ProxySelector.getDefault().select(uri); 
    } else { 
    proxyList.add(Proxy.NO_PROXY); 
    } 
    return proxyList; 
} 

Vì vậy, tôi hy vọng điều này sẽ giúp bạn.

+0

kể từ 4.0 'ProxySelectorRoutePlanner' không được chấp nhận; doc yêu cầu sử dụng 'SystemDefaultRoutePlanner'instead. Dù sao tôi sẽ đề xuất một cách mới dựa trên môi trường – boly38

1

Chỉ cần tìm thấy this answer. Cách nhanh chóng để làm điều đó là để thiết lập kế hoạch hệ thống mặc định như oleg nói:

HttpClientBuilder getClientBuilder() { 
    HttpClientBuilder clientBuilder = HttpClientBuilder.create(); 
    SystemDefaultRoutePlanner routePlanner = new SystemDefaultRoutePlanner(null); 
    clientBuilder.setRoutePlanner(routePlanner); 
    return clientBuilder; 
} 

Theo mặc định null arg sẽ được thiết lập với ProxySelector.getDefault()

Dù sao bạn có thể xác định và tùy chỉnh của riêng bạn. Một ví dụ khác ở đây: EnvBasedProxyRoutePlanner.java (gist)

Các vấn đề liên quan