Proxy problems with OkHttp

0

I'm using the OkHttp library to make my application requests for facebook api, however I need to work on one network with proxy, when instantiating OkHttpClient() and calling OkHttpClient.newCall(request).execute() I get a timeout message because my proxy for the request.

After a little searching I found the following solution :

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

This works great, however I would not like to leave the proxy information in the code or in the application.

Is there a way to configure the proxy as an environment variable or in an external file where OkHttp would be able to complete the requests?

    
asked by anonymous 22.08.2018 / 15:43

1 answer

0

I chose to use the idea of environment variables as suggested by StatelessDev

    
03.11.2018 / 22:19