How to make a http request through a socks5 proxy with authentication using android okhttp3?

123 views Asked by At

Here is my code but I always get an error

java.net.SocketException: SOCKS : authentication failed.

Indeed I have tested the credentials with a socks5 client and they are ok. Something bugs me there is only one possible value Proxy.Type.SOCKS which make no difference between SOCKS4 and SOCKS5, so does okhttp support SOCKS5 proxy in fact?

String proxyHost="replace with host";
int proxyPort=replace with proxy port;
String username="replace with username";
String password="replace with password";
String url="https://api64.ipify.org?format=json";

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

    okhttp3.Authenticator proxyAuthenticator = new okhttp3.Authenticator() {
        @Nullable
        @Override
        public Request authenticate(@Nullable Route route, @NonNull Response response) throws IOException {
            String credential = Credentials.basic(username, password);
            return response.request().newBuilder()
                    .header("Proxy-Authorization", credential)
                    .build();
        }
    };

    OkHttpClient client = new OkHttpClient.Builder()
            .connectTimeout(60, TimeUnit.SECONDS)
            .writeTimeout(60, TimeUnit.SECONDS)
            .readTimeout(60, TimeUnit.SECONDS)
            .proxy(proxyTest).proxyAuthenticator(proxyAuthenticator).build();
   

    Request request = new Request.Builder()
            .url(url)
            .build();

    Response response = client.newCall(request).execute();
0

There are 0 answers