我正在尝试使用此代码访问该网址
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();
这在我的窗口机器上工作正常,但这在linux机器上不起作用。我很喜欢这样的
线程“main”中的异常java.io.IOException:服务器返回HTTP响应代码:407为URL:http://www.google.com/ at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source) 在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 = in / etc / profile 但没有用
知道哪里出错了。
答案 0 :(得分:9)
我遇到了同样的问题。以下对我有用。
Authenticator.setDefault(new Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("username","password".toCharArray());
}
});
答案 1 :(得分: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;
}
}