OkHttp proxy settings OkHttp proxy settings json json

OkHttp proxy settings


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


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();


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 portfinal 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();