HTTPS Tomcat重新路由请求

时间:2018-02-27 20:07:33

标签: java tomcat https okhttp3

我有一个当前在Tomcat网络服务器上运行的应用程序。我的应用程序向具有IP速率限制的网站api发出https请求。

我想通过将我的https流量重新路由到代理服务器来解决这个问题,但我遇到了一些困难。

Tomcat能否通过设置-Dhttps.proxyHost和-Dhttps.proxyPort来实现这一点,还是需要在代码中实现?

我目前正在使用okhttp3和改造来发出api请求。我也可以信任使用的代理重新路由请求。

非常感谢任何帮助/指导。

1 个答案:

答案 0 :(得分:0)

很抱歉迟到的回复......

您的应用程序应该进行代理,一种方式可以是循环,即每次请求api服务器从代理地址和端口列表中获取下一个代理。比如:

public static final MediaType JSON = MediaType.parse("application/json; charset=utf-8");
  Proxy proxyTest = new Proxy(Proxy.Type.HTTP,new InetSocketAddress("proxy", proxyPort));

  OkHttpClient client = new OkHttpClient()
  .proxy(proxyTest)
  .build();

  String post(String url, String json) throws IOException {

    RequestBody body = RequestBody.create(JSON, json);
    Request request = new Request.Builder()
        .url(url)
        .post(body)
        .build();
    try (Response response = client.newCall(request).execute()) {
      return response.body().string();
    }
  }

HTH, 伽