Search code examples
javaokhttpoverpass-api

java.net.ConnectException: Failed to connect to overpass-api.de/178.63.48.217:80


Friends !!! I am new to Java & I have written my first following coding to get data thru Overpass API.

Problem Statement

But Overpass API server is not responding to Java_Servler_request (Hence Connection Timeout happens).

My Trials

  1. I tried thru Chrome Manually & I am receiving data. It means Overpass Server is working fine.
  2. I tried to call localhost (instead of Overpass API), it is responding thru OkHttp3. It means OkHttp3 is working fine.

Main code calling class

URL="http://overpass-api.de/api/interpreter";
value ="?data=way(around:100,22.36432,73.19266)[power~\"\"];(._;>;);out;";
HttpCaller API =new HttpCaller();
response.getWriter().println(API.APIGet(URL+value));

Class Calling Overpass API

import java.io.IOException;
import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class HttpCaller
{
    OkHttpClient client;
    String rspns;

    public HttpCaller()
    {
        try
        {
            client = new OkHttpClient();
            OkHttpClient.Builder builder = new OkHttpClient.Builder();
            builder.connectTimeout(30, TimeUnit.SECONDS); 
            builder.readTimeout(30, TimeUnit.SECONDS); 
            builder.writeTimeout(30, TimeUnit.SECONDS); 
            client = builder.build();
        }
        catch (Exception e) {e.printStackTrace();System.out.println(e);}
    }

    public String APIGet(String url) throws IOException
    {       
        Request request = new Request.Builder()
            .url(url)
            .build();
        System.out.println(request.url());

        try 
        { 
            Response response = null;
            System.out.println("new Call...");
            response = client.newCall(request).execute();
            System.out.println(response.code());
            rspns =  response.body().string(); 
        } catch (Exception e) {e.printStackTrace();System.out.println("Exception is "+e);} 

        return rspns;
    }
}

Result thrown

http://overpass-api.de/api/interpreter?data=way(around:100,22.36432,73.19266)[power~%22%22];(._;%3E;);out;
new Call...
java.net.ConnectException: Failed to connect to overpass-api.de/178.63.48.217:80
    at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.java:247)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.java:165)
    at okhttp3.internal.connection.StreamAllocation.findConnection(StreamAllocation.java:257)
    at okhttp3.internal.connection.StreamAllocation.findHealthyConnection(StreamAllocation.java:135)
    at okhttp3.internal.connection.StreamAllocation.newStream(StreamAllocation.java:114)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:42)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)Exception is java.net.ConnectException: Failed to connect to overpass-api.de/178.63.48.217:80
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:126)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:147)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:121)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:200)
    at okhttp3.RealCall.execute(RealCall.java:77)

Testing URL on Chrome My Desktop Snap


Solution

  • I solved my problem by setting the proxy setting for the request through OkHttp.

    Source : OkHTTPClient Proxy authentication how to?