【问题标题】:Java Jersey 2 client and proxiesJava Jersey 2 客户端和代理
【发布时间】:2014-11-27 13:25:12
【问题描述】:

我使用 Jersey 2.11 访问 confluence REST API 并从那里获取内容。

import org.codehaus.jettison.json.JSONArray;
import org.codehaus.jettison.json.JSONException;
import org.codehaus.jettison.json.JSONObject;
import org.glassfish.jersey.apache.connector.ApacheClientProperties;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;
import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.Entity;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

....

HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic(
    "confluenceUsername", "confluencePassword"
);

ClientConfig cc = new ClientConfig();
cc.property(ClientProperties.PROXY_URI, "xx.xx.xx.xx:xxxx");
Client client = ClientBuilder.newClient(cc);

client.register(feature);

WebTarget target = client.target(credential.getUrl())
    .path("rest").path("api").path("content").path(id)
    .queryParam("expand", "space,body.storage,version,container,ancestors");

Response response = target.request().get();

这是在 Jersey 2.11 中使用 Java 代理的当前正确方法吗?我总是例外

java.net.ConnectException: Connection refused: connect
javax.ws.rs.ProcessingException: java.net.Connect Exception: Connection refused: connect
    at org.glassfish.jersey.client.HttpUrlConnector.apply(HttpUrlConnector.java:229)
...

我想访问的 confluence 站点使用 https。在不需要代理的机器上使用它

HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic(
    "confluenceUsername", "confluencePassword");
Client client = ClientBuilder.newClient();
client.register(feature);

所以 Confluence 不是问题。我认为代理规范有问题,但不知道是什么。

【问题讨论】:

标签: java rest proxy jersey jersey-2.0


【解决方案1】:

我无法在 jersey 客户端中管理代理,所以我使用默认客户端创建

HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic(
    "confluenceUsername", "confluencePassword");
Client client = ClientBuilder.newClient();
client.register(feature);

然后开始我的应用程序

java -Dhttps.proxyHost=xx.xx.xx.xx -Dhttps.proxyPort:xxxx -jar myApp.jar

如果有人找到使用球衣 2.11 处理此问题的正确解决方案,请随时发布答案 :)

【讨论】:

【解决方案2】:

你能试试下面的代码吗?我使用 Jersey 2.0.1,我可以将代理设置设置为 clientConfig 对象。它正在工作

ClientConfig config = new ClientConfig();
config.connectorProvider(new ApacheConnectorProvider());
config.property(ClientProperties.PROXY_URI, "proxy_url");
config.property(ClientProperties.PROXY_USERNAME,"user_name");
config.property(ClientProperties.PROXY_PASSWORD,"password");
Client client = ClientBuilder.newClient(config);

【讨论】:

    猜你喜欢
    • 2017-01-17
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-21
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    相关资源
    最近更新 更多