LoginSignup
9
12

More than 5 years have passed since last update.

JerseyClientでプロキシを通す方法

Posted at

Apache Connector というプラグイン?を使うらしい。

build.gradle
repositories {
    mavenCentral()
}

dependencies {
    compile 'org.glassfish.jersey.core:jersey-client:2.7'
    compile 'org.glassfish.jersey.connectors:jersey-apache-connector:2.7'
}
package sample;

import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;

import org.glassfish.jersey.apache.connector.ApacheConnectorProvider;
import org.glassfish.jersey.client.ClientConfig;
import org.glassfish.jersey.client.ClientProperties;

public class Sample {
    public static void main(String[] args) {
        ClientConfig config = new ClientConfig();
        config.connectorProvider(new ApacheConnectorProvider());

        config.property(ClientProperties.PROXY_URI, "http://proxy.host.name:8080"); // プロキシの URI
        config.property(ClientProperties.PROXY_USERNAME, "username");               // プロキシ認証のユーザー名(必要なら)
        config.property(ClientProperties.PROXY_PASSWORD, "password");               // プロキシ認証のパスワード(必要なら)

        Client client = ClientBuilder.newClient(config);

        WebTarget target = client.target("https://translate.google.co.jp")
                                 .path("translate_a/t")
                                 .queryParam("client", "t")
                                 .queryParam("ie", "UTF-8")
                                 .queryParam("oe", "UTF-8")
                                 .queryParam("sl", "en")
                                 .queryParam("tl", "jp")
                                 .queryParam("otf", "1")
                                 .queryParam("q", "Hello World.");

        String response = target.request(MediaType.APPLICATION_JSON_TYPE).get(String.class);
        System.out.println(response);
    }
}
実行結果
[[["世界こんにちは。","Hello World.","Sekai kon'nichiwa.",""]],,"en",,[["世界",[1],false,false,703,0,1,0],["こんにちは。",[2],false,false,658,1,3,0]],[["World",1,[["世界",703,false,false],["ワールド",296,false,false],["世界の",0,false,false],["の世界",0,false,false],["その他の",0,false,false]],[[6,11]],"Hello World."],["Hello .",2,[["こんにちは。",658,false,false]],[[0,5],[11,12]],""]],,,[["en"]],1]

参考

9
12
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
12