设置代理

  // 设置代理的密码验证 
                Authenticator auth = new Authenticator() { 
                    private PasswordAuthentication pa =  
                        new PasswordAuthentication("username", "pwd".toCharArray()); 
                   
                    protected PasswordAuthentication getPasswordAuthentication() { 
                        return pa; 
                    } 
                }; 
                Authenticator.setDefault(auth); 
               
                Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxy.baidu.com", 8080));
               
                URL urlAddress = new URL(url);
               
                urlAddress.openConnection(proxy)


DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
    new AuthScope("PROXY HOST", 8080),
    new UsernamePasswordCredentials("username", "password"));

HttpHost targetHost = new HttpHost("TARGET HOST", 443, "https");
HttpHost proxy = new HttpHost("PROXY HOST", 8080);

httpclient.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);

你可能感兴趣的:(代理)