java.net.ConnectException: 连接天桥失败-api.de/178.63.48.217:80

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

朋友们!!!我是 Java 的新手,我已经编写了我的第一个代码以通过 Overpass API.

获取数据

问题陈述

但是 Overpass API 服务器没有响应 Java_Servler_request(因此发生连接超时)。

我的试炼

  1. 我尝试通过 Chrome 手动进行,并且我正在接收数据。这意味着 立交桥服务器 工作正常。
  2. 我试图调用 localhost(而不是 Overpass API),它通过 OkHttp3 响应。这意味着 OkHttp3 工作正常。

主要代码调用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呼唤立交桥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;
    }
}

抛出结果

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)

在 Chrome 上测试 URL My Desktop Snap

我通过 OkHttp 为请求设置代理设置...这解决了我的问题

来源: