1

I am using the OkHttp library to make requests from my application to facebook api, however I need to work on a proxy network, instantiating OkHttpClient() and calling OkHttpClient.newCall(request).execute() I get a timeout message because my proxy stop the request.

After a little research 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 want to leave the proxy information in the code or in the application.

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

Marcel
  • 125
  • 6
  • I don't think OkHttp provides any "magic" to automatically read credentials from a file or other external resources. However, it shouldn't be difficult to do it manually, for instance by reading an environment variable with [`System.getenv(name)`](https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#getenv-java.lang.String-) – user2340612 Aug 22 '18 at 13:47

1 Answers1

2

I would use system environment variables for storing this sensitive configuration. If you do not have property file, system variable would be good option.

You can update you authenticate method to this:

Authenticator proxyAuthenticator = new Authenticator() {
  @Override public Request authenticate(Route route, Response response) throws 
IOException {
       String username = System.getenv("PROXY_USERNAME");
       String password = System.getenv("PROXY_PASSWORD");
       if (username == null || username.isEmpty() || password == null || password.isEmpty() )
           throw new IllegalStateException("Proxy information is not defined in system variables");

       String credential = Credentials.basic(username, password);
       return response.request().newBuilder()
           .header("Proxy-Authorization", credential)
           .build();
  }
};

and remove

final String username = "username";
final String password = "password";

class fields.

Now when you run your application you can define the variables on the computer itself or better to pass them as parameters to you java application. For instance:

java -jar -DPROXY_USERNAME=userName -DPROXY_PASSWORD=password yourJar.jar
Igor Kanshyn
  • 867
  • 6
  • 13