18

在 Jersey1.x 上为客户端设置代理很容易:

config.getProperties().put(ApacheHttpClientConfig.PROPERTY_PROXY_URI, proxyUrl);

但是如何为 Jersey2.x 客户端添加一个 http 代理呢?我检查了源代码,但没有找到实现:

org.glassfish.jersey.client.HttpUrlConnector

谢谢!

4

6 回答 6

18

谢谢@feuyeux,解决方案对我有用,ps,下面的代码在具有http基本身份验证的代理中工作:

    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);

希望能帮助别人

于 2015-05-15T05:40:10.960 回答
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); 
于 2013-09-25T02:10:51.303 回答
9

如果您使用 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 连接器本身的说明进行操作。

于 2013-09-24T02:51:10.983 回答
5

不包含的替代方案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");
  }
}
于 2018-07-26T13:50:56.543 回答
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 );

希望有帮助:)

于 2018-01-29T11:11:09.560 回答
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
  • 注册一个新的,在底层 HttpClient 的客户端上ApacheHttpClientBuilderConfigurator设置标志.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);
于 2020-05-05T08:50:00.140 回答