Ernest Appiah
Ernest Appiah

Reputation: 133

System.err: java.net.UnknownHostException: Unable to resolve host "proxy01"

I am getting this error

System.err: java.net.UnknownHostException: Unable to resolve host "proxy01": No address associated with hostname

I am using OKHTTP. My company have two internet connections. One which requires authentication. When l make OKHTTP call using the guest network, it works as expected. But when l switch network and connect to the secured network, l get the error above. I know my company have proxy server which am suspecting is preventing outgoing network call without authentication. When l connect to the secure network and authenticate with my username and password, am able to open google.com on my phone, however, when l open my app on the phone l get the above error

Upvotes: 1

Views: 1582

Answers (1)

Sanjeev
Sanjeev

Reputation: 9946

You need to provide proxy settings and its authenticator while working with OKHTTP, Something like this :

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()
    .proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress(<proxyHost>, <proxyPort>)))
    .proxyAuthenticator(proxyAuthenticator)
    .build();

replace tokens <token> with their respective values.

Upvotes: 1

Related Questions