0

I have a problem with a Springframework project. I make a call with OkHttpClient and sometimes I get the error java.net.UnknownHostException: www.strava.com: Name or service not known

I tried to insert a 1 minute timeout thinking of solving the problem but if the call fails it fails after a few seconds The call is made successfully a few times and other times the described error returns, but the call is always the same...

    String STRAVA_URL = "https://www.strava.com/"
    String url = StravaConfig.STRAVA_URL + "/api/v3/oauth/token"; 

    RequestBody requestBody = new FormBody
        .Builder() 
        .add("client_id", StravaConfig.CLIENT_ID) 
        .add("client_secret", StravaConfig.CLIENT_SECRET) 
        .add("grant_type", "refresh_token") 
        .add("refresh_token", account.getStravaRefreshToken()) 
        .build();

    OkHttpClient client = new OkHttpClient.Builder()
            .connectTimeout(1, TimeUnit.MINUTES)
            .build();

    Request request = new Request.Builder()
            .method("POST", requestBody)
            .url(url)
            .build();

    Call call = client.newCall(request);
    Response response = null;

    try {
        response = call.execute();

        ...
    } catch (Exception e) {
        //HERE THE ERROR
        e.printStackTrace();
        return e.toString();
    }`

Solved Follow this https://serverfault.com/questions/578082/java-and-etc-resolv-conf-dns-server-failover

  • It sounds like a problem with OkHttp or DNS rather than with Spring Framework. Do other clients, such as curl on the command line, ever fail due to an unknown host? – Andy Wilkinson Jun 29 '23 at 17:24
  • command line curl is fine. How do I tell if it's a client or dns problem? – Matteo Battisti Jun 29 '23 at 21:29
  • I'd search for some existing questions about intermittent `java.net.UnknownHostException`. For example, [this question](https://stackoverflow.com/questions/24188992/unknownhostexception-from-java-but-host-resolves-with-ping-nslookup-curl) has [an answer](https://stackoverflow.com/a/24195155/1384297) with some suggestions for diagnosing a similar problem. – Andy Wilkinson Jun 30 '23 at 08:03
  • Thanks, solved follow this https://serverfault.com/questions/578082/java-and-etc-resolv-conf-dns-server-failover – Matteo Battisti Jul 05 '23 at 15:38

0 Answers0