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.
– StatelessDev
This also.
– StatelessDev
I adopted the idea of using environment variables, thanks!
– Marcel