在Jersey1.x上为客户端设置代理很容易:
config.getProperties().put(ApacheHttpClientConfig.PROPERTY_PROXY_URI, proxyUrl);
但是如何为Jersey2.x客户端添加http代理? 我检查了源代码,但没有发现实现在:
org.glassfish.jersey.client.HttpUrlConnector
谢谢!
答案 0 :(得分:18)
感谢@feuyeux,解决方案对我有用, ps,下面的代码在代理中使用http basic auth:
ClientConfig config = new ClientConfig();
config.connectorProvider(new ApacheConnectorProvider());
config.property(ClientProperties.PROXY_URI, proxy);
config.property(ClientProperties.PROXY_USERNAME,user);
config.property(ClientProperties.PROXY_PASSWORD,pass);
Client client = JerseyClientBuilder.newClient(config);
希望能帮助别人
答案 1 :(得分:11)
在运行时设置不同的代理并不是一个好方法。因此,我使用apache连接器来执行此操作:
添加定义的apache连接器依赖项:
<dependency>
<groupId>org.glassfish.jersey.connectors</groupId>
<artifactId>jersey-apache-connector</artifactId>
</dependency>
将apache连接器添加到客户端
config.property(ApacheClientProperties.PROXY_URI, proxyUrl);
Connector connector = new ApacheConnector(config);
config.connector(connector);
答案 2 :(得分:7)
如果你使用jersey 2.0默认的http连接器(这是JDK Http(s)URLConnection)。你可以简单地配置代理,如:
System.setProperty ("http.proxyHost", "proxy_server");
System.setProperty ("http.proxyPort", "proxy_port");
对于http连接器的其他实现(Apache HTTP Client和Grizzly Asynchronous Client),我之前没有尝试过。但我认为您可以按照http连接器本身的说明进行操作。
答案 3 :(得分:4)
此解决方案对我有用
<强>的pom.xml 强>
<dependency>
<groupId>org.glassfish.jersey.connectors</groupId>
<artifactId>jersey-apache-connector</artifactId>
<version>2.17</version>
</dependency>
<强>爪哇强>
ClientConfig config = new ClientConfig();
config.property( ClientProperties.PROXY_URI, "http://_YOUR_URI_:_YOUR_PORT_" );
config.connectorProvider( new ApacheConnectorProvider() );
Client client = ClientBuilder.newClient( config );
希望有所帮助:)
答案 4 :(得分:2)
标准Jersey 2.x代理配置的问题是它不允许使用nonProxyHosts选项。 它也不允许分开http和https调用,但是这些限制对我来说是可以的。
要能够重用JVM代理属性(-Dhttp.proxyHost,...),而不是指定专用的Jersey参数,可以注册特定的Jersey配置的连接器(关于前面的答案,它可能有也可能没有)过去已经开箱即用,但是当前2.30球衣版本中没有):
在maven3 pom.xml中:
<properties>
<jersey.version>2.30.1</jersey.version>
</properties>
<dependency>
<groupId>org.glassfish.jersey.connectors</groupId>
<artifactId>jersey-apache-connector</artifactId>
<version>${jersey.version}</version>
</dependency>
在您的泽西岛代码中:
ApacheConnectorProvider
ApacheHttpClientBuilderConfigurator
,它会在基础HttpClient的客户端上设置.useSystemProperties()
标志ClientConfig config = new ClientConfig()
// Apache connector to active the proxy settings
// EDIT: comment this line as it seems to be useless when using ApacheHttpClientBuilderConfigurator (below) and it provokes random hangs
//.connectorProvider(new ApacheConnectorProvider())
// Register specific features and black-magic Jersey behaviors
//.register(Xxxx.class)
//.register(Yyyy.class)
// By registering this magic lambda (Found after debugging both Jersey and HttpClient)
// We fallback on the regular JVM proxy settings properties, and avoid the restricted
// jersey properties.
//
// Jersey proxy properties are restrictive because they ignore nonProxyHosts.
// Jersey properties:
// .property(ClientProperties.PROXY_URI, "http://host:port")
// .property(ClientProperties.PROXY_USERNAME, "myProxyUser")
// .property(ClientProperties.PROXY_PASSWORD, "myProxyPassword")
//
// To be able to take into account regular JVM proxy properties:
// For HTTP: -Dhttp.proxyHost=http.proxy.example.com -Dhttp.proxyPort=10080
// For HTTPS: -Dhttps.proxyHost=https.proxy.example.com -Dhttps.proxyPort=10443
// Common for BOTH http and https: -Dhttp.nonProxyHosts=foo.example.com|bar.example.com|*baz.example.com
// Auth NTLM: -Dhttp.proxyUser=MyDomain/username or -Dhttp.auth.ntlm.domain=MyDomain
// Auth Basic: -Dhttp.proxyUser=username or -Dhttp.proxyPassword=password
.register(
((ApacheHttpClientBuilderConfigurator)
httpClientBuilder -> {
RequestConfig requestConfig =
RequestConfig.custom()
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setSocketTimeout(5000)
.build();
httpClientBuilder.setDefaultRequestConfig(requestConfig);
httpClientBuilder.useSystemProperties();
return httpClientBuilder;
}))
// Register other properties
//.property(ClientProperties.CONNECT_TIMEOUT, 5000)
//.property(ClientProperties.READ_TIMEOUT, 5000)
//.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true);
答案 5 :(得分:0)
不包含jersey-apache-connector
public class Sample {
public static void main(String[] args) {
// you can skip AUTH filter if not required
ClientConfig config = new ClientConfig(new SampleProxyAuthFilter());
config.connectorProvider(
new HttpUrlConnectorProvider().connectionFactory(new SampleConnectionFactory()));
Client client = ClientBuilder.newClient(config);
// there you go
}
}
class SampleConnectionFactory implements HttpUrlConnectorProvider.ConnectionFactory {
@Override
public HttpURLConnection getConnection(URL url) throws IOException {
return (HttpURLConnection) url
.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("host", 8080)));
}
}
class SampleProxyAuthFilter implements ClientRequestFilter {
@Override
public void filter(ClientRequestContext requestContext) throws IOException {
requestContext.getHeaders().add("Proxy-Authorization", "authentication");
}
}