Okhttp: How can I use Socks5 proxy in Okhttp to start http request ?

Created on 12 Oct 2016  路  1Comment  路  Source: square/okhttp

Sir:

Question: How can I use Socks5 proxy in Okhttp to start http request ?

My code:

Proxy proxy = new Proxy(Proxy.Type.SOCKS, InetSocketAddress.createUnresolved(
            "socks5host", 80));
    OkHttpClient client = new OkHttpClient.Builder()
           .proxy(proxy).authenticator(new Authenticator() {
                @Override
                public Request authenticate(Route route, Response response) throws IOException {
                    if (HttpUtils.responseCount(response) >= 3) {
                        return null;
                    }
                    String credential = Credentials.basic("user", "psw");
                    if (credential.equals(response.request().header("Authorization"))) {
                        return null; // If we already failed with these credentials, don't retry.
                    }
                    return response.request().newBuilder().header("Authorization", credential).build();
                }
            }).build();


    Request request = new Request.Builder().url("http://google.com").get().build();
    Response response = client.newCall(request).execute();  <--- **Here, always throw java.net.UnknownHostException: Host is unresolved: google.com**

    System.out.println(response.body().string());

How to avoid UnknownHostException?
Any example ?

Thanks!

>All comments

Please ask usage questions on StackOverflow.

Was this page helpful?
0 / 5 - 0 ratings