Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jersey client 如何为Jersey2客户端添加http代理_Jersey Client_Jersey 2.0 - Fatal编程技术网

Jersey client 如何为Jersey2客户端添加http代理

Jersey client 如何为Jersey2客户端添加http代理,jersey-client,jersey-2.0,Jersey Client,Jersey 2.0,在Jersey1.x上为客户端设置代理很容易: config.getProperties().put(ApacheHttpClientConfig.PROPERTY_PROXY_URI, proxyUrl); 但是如何为Jersey2.x客户端添加http代理呢? 我检查了源代码,没有发现实现是这样做的: org.glassfish.jersey.client.HttpUrlConnector 谢谢 如果使用jersey 2.0默认http连接器(即JDK http(s)URLConnecti

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

config.getProperties().put(ApacheHttpClientConfig.PROPERTY_PROXY_URI, proxyUrl);
但是如何为Jersey2.x客户端添加http代理呢? 我检查了源代码,没有发现实现是这样做的:

org.glassfish.jersey.client.HttpUrlConnector


谢谢

如果使用jersey 2.0默认http连接器(即JDK http(s)URLConnection)。您可以简单地配置代理,如下所示:

System.setProperty(“http.proxyHost”、“代理服务器”);
System.setProperty(“http.proxyPort”、“proxy_port”);

对于http连接器的其他实现(Apache http客户端和Grizzly异步客户端),我以前从未尝试过。但我认为您可以通过http连接器本身来遵循说明

在运行时设置不同的代理不是一个好的解决方案。因此,我使用apache connector来实现这一点:

添加定义的apache连接器依赖项:

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

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

希望能帮助他人

这个解决方案对我很有效

pom.xml

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

希望对您有所帮助:)

一个没有包含
jersey apache连接器的替代方案

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.x代理配置的问题是它不允许非代理主机选项。 它也不允许分离http和https调用,但这些限制对我来说是可以的

为了能够重用JVM代理属性(-Dhttp.proxyHost,…),而不是指定专用的Jersey参数,您可以注册特定Jersey配置的连接器(关于以前的答案,它可能在过去已经开箱了,也可能没有,但它不在当前的2.30 Jersey版本上):

  • 所有JVM代理官方属性都在官方文档中定义
  • 下面的代码通过设置useSystemProperties标志,允许回退到经典的httpclient行为
  • 跟踪更新的要点:
在maven3 pom.xml中:


2.30.1

org.glassfish.jersey.connectors
泽西apache连接器
${jersey.version}
在您的球衣代码中:

  • 添加
    ApacheConnectorProvider
  • 注册一个新的
    ApacheHttpClientBuilderConfiguration
    ,它在底层HttpClient的客户端上设置
    .useSystemProperties()
    标志
ClientConfig=newclientconfig()
//用于激活代理设置的Apache连接器
//编辑:注释这一行,因为它在使用ApacheHttpClientBuilderConfigurator(如下)时似乎没有用处,并且会引发随机挂起
//.connectorProvider(新的ApacheConnectorProvider())
//注册特定功能和黑魔法球衣行为
//.注册(Xxxx.类)
//.register(Yyyy.class)
//通过注册此magic lambda(在调试Jersey和HttpClient后找到)
//我们使用常规的JVM代理设置属性,并避免受限的
//泽西酒店。
//
//Jersey代理属性具有限制性,因为它们忽略非代理主机。
//泽西酒店:
//.property(ClientProperties.PROXY_URI,“http://host:port")
//.property(ClientProperties.PROXY_用户名,“myProxyUser”)
//.property(ClientProperties.PROXY_密码,“myProxyPassword”)
//
//要能够考虑常规JVM代理属性,请执行以下操作:
//对于HTTP:-Dhttp.proxyHost=HTTP.proxy.example.com-Dhttp.proxyPort=10080
//对于HTTPS:-Dhttps.proxyHost=HTTPS.proxy.example.com-Dhttps.proxyPort=10443
//http和https通用:-Dhttp.nonProxyHosts=foo.example.com | bar.example.com |*baz.example.com
//Auth-NTLM:-Dhttp.proxyUser=MyDomain/username或-Dhttp.Auth.NTLM.domain=MyDomain
//Auth Basic:-Dhttp.proxyUser=用户名或-Dhttp.proxyPassword=密码
.登记(
((ApacheHttpClientBuilderConfiguration)
httpClientBuilder->{
请求配置请求配置=
RequestConfig.custom()
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.setSocketTimeout(5000)
.build();
httpClientBuilder.setDefaultRequestConfig(requestConfig);
httpClientBuilder.useSystemProperties();
返回httpClientBuilder;
}))
//登记其他财产
//.property(ClientProperties.CONNECT\u超时,5000)
//.property(ClientProperties.READ_超时,5000)
//.property(HttpUrlConnectorProvider.SET\u方法\u解决方案,true);

感谢您的帮助,我认为这应该是一个很好的单一环境解决方案,但是如果我需要抽象我的biz客户端,并且代理服务器是一个参数,它不是在每个时间设置中都能工作吗?是否存在同步问题?您可以使用属性文件来配置参数。或者您可以使用-Dhttp.proxyHost=“proxy\u server”和-Dhttp.proxyPort=“proxy\u port”在运行时设置不同的代理不是好的解决方案。因此,我使用ApacheConnector来实现:org.glassfish.jersey.connectors-jersey-apache-connector-config.property(ApacheClientProperties.PROXY_-URI,proxyUrl);连接器连接器=新的ApacheConnector(配置);配置连接器(连接器);“在运行时设置不同的代理不是好的解决方案”-为什么不?如果我需要通过不同的网络建立多个连接怎么办?在这种情况下,在运行时进行设置非常重要。如果
ClientProperties,则apache connector 2.17可以正常工作。代理URI
http://
开始,谢谢Halayem Anis。你的回答真的帮助了我。到目前为止,这是唯一一个在Jersey 2代码中有效的添加代理解决方案——谢谢。这对通过TSL/SSL使用代理有效吗?我试着换成Proxy.Type.SOCKS,但它似乎停止了。我不想使用系统属性,因为我需要通过HTTPS进行一些其他调用,这些调用将
ClientConfig config = new ClientConfig();
config.property( ClientProperties.PROXY_URI, "http://_YOUR_URI_:_YOUR_PORT_" );
config.connectorProvider( new ApacheConnectorProvider() );
Client client = ClientBuilder.newClient( config );
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");
  }
}