Configure proxy to Jersey client

前端 未结 3 1175
悲哀的现实
悲哀的现实 2020-12-14 04:46

I would like to configure a proxy server to my Jersey client.
I don\'t want to configure the proxy to the whole application (using JVM arguments such as http.proxyHost),

3条回答
  •  抹茶落季
    2020-12-14 05:24

    With the help of Luca, I got it done:

    1. Implement HttpURLConnectionFactory, and override the method getHttpURLConnection, my implementation is (thanks to Luca):

      Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 3128));
      return new HttpURLConnection(url, proxy);
      
    2. Before instantiating the Jersey Client, create a new URLConnectionClientHandler, and provide your HttpURLConnectionFactory in its constructor. Then create a new Client, and provide your ClientHandler in the Client constructor. My code:

      URLConnectionClientHandler urlConnectionClientHandler = new URLConnectionClientHandler(new MyHttpURLConnectionFactory());
      _client = new Client(urlConnectionClientHandler);
      

    Hope that's help.

提交回复
热议问题