Proxy problems with Okhttp

Asked

Viewed 75 times

0

I’m using the library Okhttp to request my application to the facebook api, but I need to work on a proxy network, when instantiating OkHttpClient() and call OkHttpClient.newCall(request).execute() i get a timeout message because my proxy for the request.

After researching a little 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 very well, however I would not like to leave the proxy information in the code or in the application.

Is there any way to configure the proxy as an environment variable or in some external file where Okhttp could complete the requests?

  • This maybe it’ll help you.

  • 1

    This also.

  • I adopted the idea of using environment variables, thanks!

1 answer

0


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

Browser other questions tagged

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