【问题标题】:Add Proxy to restlet ClientRessource添加代理到 restlet ClientRessource
【发布时间】:2018-05-03 11:14:45
【问题描述】:

我正在尝试将代理设置添加到 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();
}

编辑添加编译:

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)

【问题讨论】:

    标签: restlet


    【解决方案1】:

    我认为这仅受 httpClient 扩展支持,它依赖于 Apache HTTP 客户端库(maven 工件 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");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多