将代理添加到restlet ClientRessource

时间:2018-05-03 11:14:46

标签: restlet

我正在尝试将代理设置添加到Java Swing客户端应用程序,该应用程序通过https从外部服务器连接并获取数据。但是ClientResource(restlet:2.4.0)忽略了所有带参数的工作并直接连接到url?如果语法正确,那么正确的参数是什么? 此外,我如何使用系统代理设置?

private static ClientResource getClientResource(String url) {

    ClientResource clientResource = null;

    try {
        // test
        Client client = new Client(new Context(), Protocol.HTTPS);
        client.getContext().getParameters().add("https.proxyHost", "PROXY_IP");
        client.getContext().getParameters().add("https.proxyPort", "PROXY_PORT");

        clientResource = new ClientResource(url);

        // test
        clientResource.setNext(client);
    } catch (Exception e) {
        e.printStackTrace();
    }

    return clientResource;
}


private static Response sendGetRequest(String url) {
    ClientResource resource = getClientResource(BASE_URL + url);

    try {
        resource.get();
    } catch (ResourceException e){
        e.printStackStrace();
        return null;
    }
    return getResponse();
}

EDIT添加了编译:

compile 'org.restlet.jse:org.restlet:2.3.12'
compile 'org.restlet.jse:org.restlet.ext.jackson:2.3.12'
// switch to Apache Http Client, enable proxy'
compile 'org.restlet.jse:org.restlet.ext.httpclient:2.3.12' 
// httpClient for Class Definitions
compile 'org.apache.httpcomponents:httpclient:4.3' 

当前例外:

Starting the Apache HTTP client
An error occurred during the communication with the remote HTTP server.

org.apache.http.client.ClientProtocolException
    at org.apache.http.impl.client.AbstractHttpClient.doExecute(AbstractHttpClient.java:867)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:82)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:106)
    at org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57)
    at org.restlet.ext.httpclient.internal.HttpMethodCall.sendRequest(HttpMethodCall.java:339)
    at org.restlet.engine.adapter.ClientAdapter.commit(ClientAdapter.java:105)
    at org.restlet.engine.adapter.HttpClientHelper.handle(HttpClientHelper.java:119)
    at org.restlet.Client.handle(Client.java:153)

1 个答案:

答案 0 :(得分:0)

我认为这仅支持httpClient扩展,它依赖于Apache HTTP客户端库(maven artifact id:org.restlet.ext.httpclient)。

然后,您可以使用系统环境属性:http.proxyHosthttp.proxyPort,或者在客户端实例上设置这些参数(就像您一样,但名称是不同的并记录在案here

Client client = new Client(new Context(), Protocol.HTTPS);
client.getContext().getParameters().add("proxyHost", "PROXY_IP");
client.getContext().getParameters().add("proxyPort", "PROXY_PORT");