--sunfruit
在openConnection之前加上
Properties prop = System.getProperties();
System.getProperties().put("proxySet","true");
// 設(shè)置http訪(fǎng)問(wèn)要使用的代理服務(wù)器的地址
prop.setProperty("http.proxyHost","xxxxxxx");
// 設(shè)置http訪(fǎng)問(wèn)要使用的代理服務(wù)器的端口
prop.setProperty("http.proxyPort","xxxxx");
然后并不像有的貼子說(shuō)的那樣添加
prop.setProperty("http.proxyUser","xxxxxxxx");
prop.setProperty("http.proxyPassword","xxxxx");
添加這樣的內(nèi)容是不行的,而是將"username:password"進(jìn)行base64編碼,具體代碼如下
String authentication = "username:password";
String encodedLogin = new BASE64Encoder().encode(authentication.getBytes());
httpurlconnection.setRequestProperty("Proxy-Authorization", " Basic " + encodedLogin);
注意BASE64Encoder().encode(authentication.getBytes());這里不能使用BASE64Encoder().encodeBuffer(authentication.getBytes())否則報(bào)錯(cuò)
具體原因參考sun公布的JDK BUG , Bug ID: 4615330
http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4615330