java.net.Proxy problem with Marshmallow and versions below

Asked

Viewed 39 times

0

I have the following test :

package com.technopartner.technosdk.proxy;


import android.support.test.runner.AndroidJUnit4;

import org.junit.Test;
import org.junit.runner.RunWith;

import java.io.IOException;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
import java.net.Proxy;
import java.net.ProxySelector;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;

import static junit.framework.Assert.assertTrue;

@RunWith(AndroidJUnit4.class)
public class OkHttpClientTest {

    private static int PORT = 1080;
    private static  String[] IPS = {"127.0.0.10", "127.0.0.11","127.0.0.12"};
    private static String USER_NAME = "USUARIO";
    private static String PASSWORD = "SENHA";
    private static String URL_CONNECT = "https://www.google.com/";

    private final Authenticator authenticator = new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(USER_NAME, PASSWORD.toCharArray());
        }
    };

    @Test
    public void testSocksFunctionality() throws IOException {

        // set Authenticator
        Authenticator.setDefault(authenticator);

        // Create socket...
        final Socket socket = new Socket(IPS[0], PORT);
        assertTrue(socket.isConnected());

        //create URL...
        final URL url = new URL(URL_CONNECT);
        InetSocketAddress  socketAddress = new InetSocketAddress(IPS[0], PORT);
        // create Proxy
        final Proxy proxy = new Proxy( Proxy.Type.SOCKS, socketAddress );

        // Open connection...
        final URLConnection urlConnection = url.openConnection(proxy);
        urlConnection.connect();

        assertTrue( urlConnection.getDoInput() );

        final OkHttpClient client = new OkHttpClient.Builder()
                .proxy(proxy)
                .retryOnConnectionFailure(false)
                .build();

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

        assertTrue(client.newCall(request).execute().isSuccessful());
    }

}

This performs connection testing using a Proxy.

When I run the test in API’s below 24, the following error occurs:

java.net.UnknownHostException: Host is unresolved: www.google.com
at java.net.Socket.connect(Socket.java:865)
at okhttp3.internal.platform.AndroidPlatform.connectSocket(AndroidPlatform.java:63)
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:223)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:149)
at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:192)
at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:121)
at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:100)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)

How do I make connections using Proxy in versions below API 24 ?

1 answer

0

In principle the versions below API 24 is not to influence this type of connection.

However, if you are using exactly API 24 or above, you may be experiencing security issues such as certificates, so you may need to make some settings.

If this is the case, I recommend reading this guide to Android Developers:

Network security settings

Back to the question, try modifying your code for something in this template:

int proxyPort = 8080;
String proxyHost = "proxyHost";
final String username = "username";
final String password = "password";

Authenticator proxyAuthenticator = new Authenticator() {
  @Override public Request authenticate(Route route, 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(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)))
    .proxyAuthenticator(proxyAuthenticator)
    .build();

Also check your file Manifest has the permission: <uses-permission android:name="android.permission.INTERNET" />

Source: Okhttpclient Proxy Authentication how to?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.