2016-07-14 156 views
0

我遇到的技術問題連接到googleapis Java程序java.net.ConnectException:連接與谷歌地圖拒絕

String url = https://maps.googleapis.com/maps/api/geocode/json?address=Ctra.+Ibiza+a+San+Antonio%2C+Km+5%2C+07816+San+Rafael%2C+Illes+Balears%2C+Spain&key=AIzaSyAawKhhK1ieNloq0wogoSTXb-eEwpb-pol 

URL url = new URL(url); 
URLConnection uc = url.openConnection(); 
uc.connect(); 

錯誤內部時拒絕連接錯誤:

Exception in thread "main" java.net.ConnectException: Connection refused: connect 
    at java.net.DualStackPlainSocketImpl.connect0(Native Method) 
    at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) 
    at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:339) 
    at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:200) 
    at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:182) 
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) 
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) 
    at java.net.Socket.connect(Socket.java:579) 
    at sun.security.ssl.SSLSocketImpl.connect(SSLSocketImpl.java:618) 
    at sun.security.ssl.BaseSSLSocketImpl.connect(BaseSSLSocketImpl.java:160) 
    at sun.net.NetworkClient.doConnect(NetworkClient.java:180) 
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:432) 
    at sun.net.www.http.HttpClient.openServer(HttpClient.java:527) 
    at sun.net.www.protocol.https.HttpsClient.<init>(HttpsClient.java:275) 
    at sun.net.www.protocol.https.HttpsClient.New(HttpsClient.java:371) 
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.getNewHttpClient(AbstractDelegateHttpsURLConnection.java:191) 
    at sun.net.www.protocol.http.HttpURLConnection.plainConnect(HttpURLConnection.java:932) 
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:177) 
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:153) 

和代理設置有明確的定義:

Authenticator.setDefault(
        new Authenticator() { 
         @Override 
         public PasswordAuthentication getPasswordAuthentication() { 
         return new PasswordAuthentication(
           "estebbe", "5kjk61a".toCharArray()); 
         } 
        } 
       ); 

System.setProperty("http.proxyHost", "127.27.237.33"); 
System.setProperty("http.proxyPort", "8022"); 
System.setProperty("http.nonProxyHosts", "localhost|127.0.0.1"); 

並將該網址放入瀏覽器中,你好!

回答

0

而不是使用URLConnection嘗試使用HttpURLConnection,並且添加以下請求屬性。

HttpURLConnection urlConnection = (HttpURLConnection) postURL.openConnection(); 
urlConnection.addRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.86 Safari/537.36"); 
+0

相同結果:-( –

相關問題