public class ProxyConnTest {    public static void initProxy(String host, int port, final String username,
            final String password) {
        Authenticator.setDefault(new Authenticator() {            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username,
                        new String(password).toCharArray());
            }
        });
        System.setProperty("proxyType", "4");
        System.setProperty("proxyPort", Integer.toString(port));
        System.setProperty("proxyHost", host);
        System.setProperty("proxySet", "true");
    }    public static void main(String[] args) throws IOException {
        String url = "http://java.sun.com/";
        String proxy = "172.18.1.11";
        int port = 8080;
        String username = "name";
        String password = "pwd";
        String curLine = "";
        String content = "";
        URL server = new URL(url);
        initProxy(proxy, port, username, password);
        HttpURLConnection connection = (HttpURLConnection) server
                .openConnection();
        connection.connect();
        InputStream is = connection.getInputStream();
        BufferedReader reader = new BufferedReader(new InputStreamReader(is));
        while ((curLine = reader.readLine()) != null) {
            content += curLine;
        }
        System.out.println("content= " + content);
        is.close();
    }
}  
网上借鉴了代码,但是运行了一下代码以后就报错:
Exception in thread "main" java.net.ProtocolException: Server redirected too many  times (20)
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(Unknown Source)
at com.test.ibm.ProxyConnTest.main(ProxyConnTest.java:42)不知道是什么原因?
想实现的是通过通过代理验证,能够访问外网

解决方案 »

  1.   

            HttpURLConnection connection = (HttpURLConnection) server.openConnection();
            connection .setFollowRedirects(true);
            connection .setInstanceFollowRedirects(false);
            connection.connect();应该是redirect次数超过限制的问题,祝你好运!
      

  2.   

    用户名密码错误  username 要写成 “DOMAIN\\name” DOMAIN是“域” 不知道域是什么问你们网管