5

我想通過身份驗證代理獲取URL的內容。這是我想使用的代碼:HttpUrlConnection代理身份驗證進入重定向循環

 Authenticator authenticator = new Authenticator() { 
      public PasswordAuthentication getPasswordAuthentication() { 
       System.out.println("authenticating"); 
       return (new PasswordAuthentication("username", "password".toCharArray())); 
      } 
     }; 
     Authenticator.setDefault(authenticator); 
     URL url = new URL("http://www.google.com"); 
     InetSocketAddress proxyAddress = new InetSocketAddress("address.of.proxy", 6060); 
     Proxy proxy = new Proxy(Proxy.Type.HTTP, proxyAddress); 
     HttpURLConnection uc = (HttpURLConnection) url.openConnection(proxy); 
     uc.connect(); 
     System.out.println(uc.getResponseCode()); 

出於某種原因,該認證進入重定向循環,所以結果是認證打印「認證」的20倍,那麼的ProtocolException

java.net.ProtocolException: Server redirected too many times (20) 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1846) 
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1439) 
at java.net.URLConnection.getContent(URLConnection.java:739) 
at proxytest.RunThis.main(RunThis.java:29) 

代理正在使用給定的憑據,我已通過瀏覽器嘗試過。 我試圖讓這工作幾天,我已經嘗試設置系統屬性,Apache httpclient和任何我可以離開谷歌。 任何想法讚賞。 :)

UPDATE:

我使用Wireshark測試,代理驗證信息在請求,但代理拋出回407錯誤。再一次,憑證是好的,它的工作完美的瀏覽器(我實際上從源代碼複製它們來確保)。

雖然我注意到了一件事。 Proxy-Authorization頭的值在瀏覽器和java發送的請求之間只有一個字符。這是否意味着什麼?

+0

我有完全相同的問題......你有解決方案或任何新的想法? – Ben

+0

進一步:我讀了關於使用cookie-hander,但即使啓用默認cookie處理程序「CookieHandler.setDefault(新的CookieManager(null,CookiePolicy.ACCEPT_ALL));'我得到相同的結果... – Ben

+0

@Koocka嗨,你讓它工作? – Inoy

回答