sunfruit[請訪問http://www.fruitres.cn]

          --我相信JAVA能走得更遠(yuǎn) QQ:316228067

          [原創(chuàng)]通過HttpURLConnection如何使用代理服務(wù)器連接公網(wǎng)

              --sunfruit

              在openConnection之前加上
              Properties   prop   =   System.getProperties();
              System.getProperties().put("proxySet","true");
              // 設(shè)置http訪問要使用的代理服務(wù)器的地址
              prop.setProperty("http.proxyHost","xxxxxxx");
              // 設(shè)置http訪問要使用的代理服務(wù)器的端口
              prop.setProperty("http.proxyPort","xxxxx");
             
              然后并不像有的貼子說的那樣添加
              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())否則報錯
              具體原因參考sun公布的JDK BUG , Bug ID:  4615330  http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4615330

          posted on 2007-03-22 10:41 sunfruit 閱讀(1785) 評論(0)  編輯  收藏 所屬分類: JAVA SE & EE

          主站蜘蛛池模板: 凉城县| 客服| 瑞丽市| 正安县| 延川县| 平泉县| 建宁县| 文水县| 桂东县| 泽普县| 象山县| 托克逊县| 苍山县| 远安县| 沾益县| 于田县| 怀来县| 响水县| 松阳县| 瑞金市| 乌海市| 织金县| 湛江市| 涡阳县| 朝阳县| 湘西| 宁夏| 商洛市| 黎城县| 保山市| 郎溪县| 惠州市| 准格尔旗| 禹城市| 德格县| 额济纳旗| 德安县| 绥宁县| 许昌县| 镇沅| 尼勒克县|