33

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

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:

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

Iam using the 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();

But nothing works so far.

Thanks for your time!

Matheus Bica
  • 1,055
  • 3
  • 13
  • 23
  • You were calling [OkHttpClient.proxy()](http://square.github.io/okhttp/3.x/okhttp/okhttp3/OkHttpClient.html#proxy--) But you want to call [OkHttpClient.Builder.proxy(Proxy)](http://square.github.io/okhttp/3.x/okhttp/okhttp3/OkHttpClient.Builder.html#proxy-java.net.Proxy-). – Brent Bradburn Oct 27 '17 at 16:02
  • 2
    You can set the proxy that the JVM uses, with the flags (e.g.) `java -Dhttp.proxyHost=10.0.0.100 -Dhttp.proxyPort=8800 myclass` (Useful for when okHttp change their api AGAIN). – Steve Smith Aug 21 '19 at 12:36

3 Answers3

38

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

rajadilipkolli
  • 3,475
  • 2
  • 26
  • 49
Matheus Bica
  • 1,055
  • 3
  • 13
  • 23
  • For mocking unit test reasons, I need to pass the OkHttpClient() class from the constructor args. So I am not able to add a proxy for the OkHttpClient() with the builder. Is there any other way to use the object of OkHttpClient() instead of new OkHttpClient.Builder() ? – Kishor kumar R May 07 '21 at 11:23
  • okHttpClient.newBuilder().proxy(proxy).build(); worked for the objects ( without new OkHttpClient() ) – Kishor kumar R May 07 '21 at 11:44
10

okhttp version:3.11.0. SOCKS proxy example

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();
duyuanchao
  • 3,863
  • 1
  • 25
  • 16
  • Thank you for your example, how can I use auth for socks5 in the example above? please add code sample if you can – vasilevich Sep 20 '18 at 04:49
  • visit this: https://stackoverflow.com/questions/35554380/okhttpclient-proxy-authentication-how-to @vasilevich – duyuanchao Sep 20 '18 at 06:44
  • 1
    thank you for the suggestion but the example provided in the link works only with HTTP proxy, not socks5, I already tried. – vasilevich Sep 20 '18 at 09:39
8

SOCKS5 Auth example

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 here and 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)

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();
Eugene Kortov
  • 445
  • 6
  • 17