2013-06-19 133 views
0

我试图通过使用此代码HTTP错误407需要代理授权

System.setProperty("http.proxyHost", "111.88.15.108"); 
    System.setProperty("http.proxyPort", "8002"); 
    System.setProperty("http.proxyUser", "user"); 
    System.setProperty("http.proxyPassword", "password"); 
    URL oracle = new URL("http://www.google.com/"); 
    URLConnection yc = oracle.openConnection(); 
    BufferedReader in = new BufferedReader(new InputStreamReader(
      yc.getInputStream())); 
    String inputLine; 
    while ((inputLine = in.readLine()) != null) 

     System.out.println(inputLine); 
    in.close(); 

这是在我的窗口机器工作正常访问的URL,但这不是在Linux机器上运行。我得到eror像线程 “主” 产生java.io.IOException这个

例外:服务器返回的HTTP响应代码:407网址:http://www.google.com/ 在sun.net.www.protocol.http.HttpURLConnection .getInputStream(来源不明) 在com.yahoo.Connection.main(Connection.java:31)

即使是代理设置是正确的,我想这样也

java -Dhttp.proxyHost="111.88.15.108" -Dhttp.proxyPort="8002" -Dhttp.proxyUser="user" -Dhttp.proxyPassword="password" -jar yahoo_test3.jar 

但同样的错误,我试图设置出口HTTP_PROXY =在/ etc/profile文件,但没有用

任何想法,这是怎么了。

+1

你可能需要在你的Linux机器设置为使用您公司的Active Directory身份验证系统。 – fge

回答

7

我有同样的问题。以下为我工作。

Authenticator.setDefault(new Authenticator() 
{ 
    protected PasswordAuthentication getPasswordAuthentication() 
    { 
    return new PasswordAuthentication("username","password".toCharArray()); 
    } 
}); 
+0

终于,这对我很好用 – user1583465

+0

这也适用于我。 :) – Paulo

0

以下为我工作

public class TEST4 { 

    public static void main(String[] args) throws IOException { 

     System.setProperty("http.proxyHost", "147.67.217.23"); 
     System.setProperty("http.proxyPort", "8012"); 
     URL url=new URL("http://stackoverflow.com"); 
     URLConnection uc = url.openConnection(); 
     String encoded = new String (base64Encode(new String("pecador:d8kjk69t"))); 
     uc.setRequestProperty("Proxy-Authorization", "Basic " + encoded); 
     uc.connect(); 

     BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream())); 
     String inputLine; 
     while ((inputLine = in.readLine()) != null) 
      System.out.println(inputLine); 
     in.close(); 

    } 

    public static String userNamePasswordBase64(String username, String password) { 
     return "Basic " + base64Encode(username + ":" + password); 
    } 

    private final static char base64Array[] = { 'A', 'B', 'C', 'D', 'E', 'F', 
      'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 
      'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 
      'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 
      't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', 
      '6', '7', '8', '9', '+', '/' }; 

    private static String base64Encode(String string) { 
     String encodedString = ""; 
     byte bytes[] = string.getBytes(); 
     int i = 0; 
     int pad = 0; 
     while (i < bytes.length) { 
      byte b1 = bytes[i++]; 
      byte b2; 
      byte b3; 
      if (i >= bytes.length) { 
       b2 = 0; 
       b3 = 0; 
       pad = 2; 
      } else { 
       b2 = bytes[i++]; 
       if (i >= bytes.length) { 
        b3 = 0; 
        pad = 1; 
       } else 
        b3 = bytes[i++]; 
      } 
      byte c1 = (byte) (b1 >> 2); 
      byte c2 = (byte) (((b1 & 0x3) << 4) | (b2 >> 4)); 
      byte c3 = (byte) (((b2 & 0xf) << 2) | (b3 >> 6)); 
      byte c4 = (byte) (b3 & 0x3f); 
      encodedString += base64Array[c1]; 
      encodedString += base64Array[c2]; 
      switch (pad) { 
      case 0: 
       encodedString += base64Array[c3]; 
       encodedString += base64Array[c4]; 
       break; 
      case 1: 
       encodedString += base64Array[c3]; 
       encodedString += "="; 
       break; 
      case 2: 
       encodedString += "=="; 
       break; 
      } 
     } 
     return encodedString; 
    } 

} 
相关问题