Java OkHttp 代理设置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37866902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 19:44:37  来源:igfitidea点击:

OkHttp proxy settings

javajsonpostproxyokhttp3

提问by Matheus Bica

I have to setup a proxy to send a JSON using POST, using proxyHost and proxyPort.

我必须设置一个代理以使用 POST、proxyHost 和 proxyPort 发送 JSON。

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();
  //OkHttpClient.Builder builder = new OkHttpClient.Builder();
  //builder.proxy(proxySAP);
  //client.setProxy(proxySAP)
  //OkHttpClient client = builder.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();
    }
  }

When i try to use the proxyTest that I've saw on some answers here it points an error:

当我尝试使用我在某些答案中看到的 proxyTest 时,它指出了一个错误:

The method proxy() in the type OkHttpClient is not applicable for the arguments (Proxy)

OkHttpClient 类型中的方法 proxy() 不适用于参数 (Proxy)

Iam using the OKHTTP 3.3.1(okhttp3)

我正在使用 OKHTTP 3.3.1(okhttp3)

My question is, what should I do? I did some tests like this:

我的问题是,我该怎么办?我做了一些这样的测试:

OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.proxy(proxyTest);
client.setProxy(proxyTest)
OkHttpClient client = builder.build();

OkHttpClient.Builder builder = new OkHttpClient.Builder();
builder.proxy(proxyTest);
client.setProxy(proxyTest)
OkHttpClient 客户端 = builder.build();

But nothing works so far.

但到目前为止没有任何效果。

Thanks for your time!

谢谢你的时间!

采纳答案by Matheus Bica

Found the solution:

找到了解决办法:

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

If we use the builder to input the proxy, it will work like a charm =D

如果我们使用构建器输入代理,它将像魅力一样工作 =D

回答by shellhub

okhttp version:3.11.0. SOCKS proxy example

okhttp 版本:3.11.0. SOCKS 代理示例

String hostname = "localhost"/*127.0.0.1*/;
int port = 1080;
Proxy proxy = new Proxy(Proxy.Type.SOCKS,
        new InetSocketAddress(hostname, port));

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

回答by Eugene Kortov

SOCKS5 Auth example

SOCKS5 身份验证示例

I think it's the easiest working soulution. But it seems to me that it can be not 100% safe. I took this code from this code from hereand modified it because my proxy's RequestorType is SERVER. Actually, java has a strange api for proxies, you should to set auth for proxy through system env ( you can see it from the same link)

我认为这是最简单的工作解决方案。但在我看来,它可能不是 100% 安全的。我从这里的代码中获取了这段代码并对其进行了修改,因为我的代理的 RequestorType 是 SERVER。实际上,java有一个奇怪的代理api,您应该通过系统环境为代理设置auth(您可以从同一个链接中看到它)

final int proxyPort = 1080; //your proxy port
final String proxyHost = "your proxy host";
final String username = "proxy username";
final String password = "proxy password";

InetSocketAddress proxyAddr = new InetSocketAddress(proxyHost, proxyPort);
Proxy proxy = new Proxy(Proxy.Type.SOCKS, proxyAddr);

Authenticator.setDefault(new Authenticator() {
  @Override
  protected PasswordAuthentication getPasswordAuthentication() {
    if (getRequestingHost().equalsIgnoreCase(proxyHost)) {
      if (proxyPort == getRequestingPort()) {
        return new PasswordAuthentication(username, password.toCharArray());
      }
    }
    return null;
  }
});


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