如何防止Java客户机使用系统代理?
这段代码使用代理,读取网页并将其作为字符串返回如何防止Java客户机使用系统代理?,java,proxy,Java,Proxy,这段代码使用代理,读取网页并将其作为字符串返回 public String getPageFromProxy(String urlAdress) { try { URL url = new URL(urlAdress); Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(System.getProperty("http.proxyHost"), Integer.value
public String getPageFromProxy(String urlAdress) {
try {
URL url = new URL(urlAdress);
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(System.getProperty("http.proxyHost"), Integer.valueOf(System.getProperty("http.proxyPort"))));
HttpURLConnection uc = (HttpURLConnection)url.openConnection(proxy);
String authString = System.getProperty("http.proxyUser") + ":" + System.getProperty("http.proxyPassword");
authString = Text.getBase64Encoded(authString);
uc.setRequestProperty("Proxy-Authorization", authString);
uc.connect();
InputStream in = url.openStream();
StringBuilder sb = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String read;
while ((read = br.readLine()) != null) {
sb.append(read);
}
br.close();
return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
正如您所看到的,我已经做了很多尝试,以使其真正使用具有某些给定凭据的代理。我还按照stackoverflow上的建议覆盖了java.net.Authenticator。但是,事实证明Java客户端不提供任何凭据。该代码仅在客户端在代理给出407回答后从用户获取系统代理设置时起作用
这确实显示为空
System.out.println("Proxy is system " + System.getProperty("java.net.useSystemProxies"));
如何在Java7中正确地设置代理,使其仅使用给定的凭据
感谢您提供有关调试的任何提示 为了建立代理连接,您已经做了很多工作,但现在却忽略了这一切,并打开了一个全新的连接:
InputStream in = url.openStream();
您可能希望在代理连接上打开输入流:
InputStream in = uc.getInputStream();