【问题标题】:How to add a http proxy for Jersey2 Client如何为 Jersey2 客户端添加 http 代理
【发布时间】:2013-09-22 10:06:46
【问题描述】:

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

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

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

org.glassfish.jersey.client.HttpUrlConnector

谢谢!

【问题讨论】:

    标签: jersey-client jersey-2.0


    【解决方案1】:

    感谢@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);
    

    希望能帮助别人

    【讨论】:

      【解决方案2】:

      在运行时设置不同的代理不是好的解决方案。因此,我使用 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); 
      

      【讨论】:

      • 如果ClientProperties.PROXY_URIhttp:// 开头,它可以与apache 连接器2.17 一起正常工作
      【解决方案3】:

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

      【讨论】:

      • 感谢您的帮助,我认为这应该是单一环境的良好解决方案,但是如果我需要抽象我的biz客户端,并且代理服务器是一个参数,它不是每次都有效环境?有同步问题吗?
      • 您可以使用属性文件来配置参数。或者你可以使用 -Dhttp.proxyHost="proxy_server" 和 -Dhttp.proxyPort="proxy_port"
      • 在运行时设置不同的代理不是好的解决方案。因此,我使用 apache 连接器来执行此操作: org.glassfish.jersey.connectorsjersey-apache-connector config.property(ApacheClientProperties.PROXY_URI , 代理网址);连接器连接器 = 新的 ApacheConnector(config); config.connector(connector);
      • “在运行时设置不同的代理不是好的解决方案” - 为什么不呢?如果我需要通过不同的网络建立多个连接怎么办?在这种情况下,运行时设置是必不可少的
      【解决方案4】:

      不包含 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");
        }
      }
      

      【讨论】:

      • 到目前为止,这是唯一适用于 Jersey 2 代码的添加代理解决方案 -- 谢谢。
      • 这是否适用于通过 TSL/SSL 使用代理?我尝试更改为 Proxy.Type.SOCKS ,但它似乎只是停滞不前。我不想使用系统属性,因为我需要通过 HTTPS 进行一些其他不会使用代理的调用。
      • @mekane84,Proxy.Type.HTTP表示代理服务器是http,当我用它访问https(TSL/SSL)服务器时它工作正常。
      【解决方案5】:

      这个解决方案对我有用

      pom.xml

      <dependency>
          <groupId>org.glassfish.jersey.connectors</groupId>
          <artifactId>jersey-apache-connector</artifactId>
          <version>2.17</version>
      </dependency>
      

      Java

      ClientConfig config = new ClientConfig();
      config.property( ClientProperties.PROXY_URI, "http://_YOUR_URI_:_YOUR_PORT_" );
      config.connectorProvider( new ApacheConnectorProvider() );
      Client client = ClientBuilder.newClient( config );
      

      希望有帮助:)

      【讨论】:

      • 谢谢你 Halayem Anis。你的回答对我很有帮助。
      【解决方案6】:

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

      【讨论】:

        猜你喜欢
        • 2015-02-09
        • 2013-01-17
        • 1970-01-01
        • 1970-01-01
        • 2016-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-02-28
        相关资源
        最近更新 更多