Подтвердить что ты не робот

Как добавить HTTP-прокси для клиента Jersey2

Легко установить прокси для клиента на Jersey1.x:

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

Но как добавить HTTP-прокси для клиента Jersey2.x? Я проверил исходный код и не нашел реализацию в том, что:

org.glassfish.jersey.client.HttpUrlConnector

Спасибо!

4b9b3361

Ответ 1

Чтобы установить другой прокси-сервер во время выполнения, это нехорошее решение. Соответственно, я использовал соединитель 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

Спасибо @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);

надеемся помочь другим

Ответ 3

Если вы используете jersey 2.0 default http connector (который является JDK Http (-ами) URLConnection). Вы можете просто настроить прокси:

    System.setProperty ("http.proxyHost", "proxy_server");
    System.setProperty ("http.proxyPort", "proxy_port");

Для других реализаций http-коннектора (Apache HTTP Client и Grizzly Asynchronous Client) я раньше не пробовал. Но я думаю, что вы могли бы следовать инструкциям через сам соединитель http.

Ответ 4

Это решение сработало для меня

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

Надеюсь, что помогает:)