2013-07-03 48 views
1

我正在嘗試訪問https://www.api.org/。 使用下面的代碼,但它給出了一些錯誤。 但我可以打一個谷歌API HTTP URL和它工作得很好,沒有任何錯誤無法通過代理隧道。代理服務器返回「HTTP/1.1 407代理授權要求」

DataInputStream di = null; 

FileOutputStream fo = null; 

byte[] b = new byte[1]; 

// PROXY 
System.setProperty("https.proxyHost", "my proxy"); 
System.setProperty("https.proxyPort", "8080"); 


Authenticator.setDefault(new PasswordAuthenticator()); 


URL u = new URL("https://maps.googleapis.com/maps/api/place/autocomplete/json?input=Vict&types=geocode&language=fr&sensor=true&key=keyforuse"); 

HttpURLConnection con = (HttpURLConnection) u.openConnection(); 

sun.misc.BASE64Encoder encoder = new sun.misc.BASE64Encoder(); 
String encodedUserPwd = encoder.encode("domain\\user:password" 
     .getBytes()); 
con.setRequestProperty("Proxy-Authorization", "Basic " 
     + encodedUserPwd); 

di = new DataInputStream(con.getInputStream()); 
// result = parseJSON(di); 
while (-1 != di.read(b, 0, 1)) { 
    System.out.print(new String(b)); 
} 

但得到以下

java.io.IOException: Unable to tunnel through proxy. Proxy returns "HTTP/1.1 407 Proxy Authorization Required" 
    at sun.net.www.protocol.http.HttpURLConnection.doTunneling(HttpURLConnection.java:1648) 
    at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:164) 
    at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1172) 
    at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:234) 
    at kf.store.locator.googlemaps.test.main(test.java:58) 

任何幫助錯誤????

回答

0

您可能不需要用戶名前面的域。我有一個非常類似的問題,當我在用戶名前面刪除域名時,我對它進行了修復。由於代理服務器在域上,我認爲這意味着你在同一個域下。

+0

我已刪除域和使用以下sun.misc.BASE64Encoder編碼器的代碼=新sun.misc.BASE64Encoder(); \t \t \t字符串encodedUserPwd = encoder.encode( 「386654:軍1 @ 2013」​​ \t \t \t \t \t .getBytes()); \t \t \t con.setRequestProperty( 「代理授權」, 「基本」 \t \t \t \t \t + encodedUserPwd);仍然出現錯誤無法通過代理隧道。代理服務器返回「HTTP/1.1 400錯誤請求」任何幫助將被授予 – rand

+0

而不是添加編碼的憑據,您是否嘗試將它們添加到代理主機? System.setProperty(「https.proxyHost」,「user:pass @ proxyHost」) 不知道這是否有用,但值得一試。之前我已經擺脫了這個問題。除此之外,你的代碼對我來說看起來不錯。 – Callan

0

我通過使用我的用戶名和密碼向代理服務器進行身份驗證來修復它。如果你在公司的網絡上,它也可以成爲你的信用。 這樣做的等效將是這樣的:

-Dhttp.proxyUser=myusername 
-Dhttp.proxyPassword=mypassword 
-Dhttp.proxyHost=myproxyserver.com 
-Dhttp.proxyPort=9000 
相關問題