代理服务器的设置是通过设置系统属性来完成的:
System.getProperties().put("proxySet", "true");
System.getProperties().put("proxyHost", YourProxyHost);
System.getProperties().put("proxyPort", YourProxyPort); 代理服务器的验证则是通过设置Http请求头来完成的。HttpURLConnection con = (HttpURLConnection)(new URL(url)).openConnection();
con.setRequestProperty("Proxy-Authorization", "Basic " + Encoder.base64Encode(ProxyUserName + ":" + ProxyUserPass));

解决方案 »

  1.   

    import java.io.*;
    import java.net.*;
    public class textwhile{
    public static void main(String args[]) throws MalformedURLException,IOException{
    String urlString,username,password; System.getProperties().setProperty( "http.proxyHost", "202.119.24.35" );
    System.getProperties().setProperty( "http.proxyPort", "8080" ); if(args.length!=3){
    System.err.println("Usage: java AuthDemo URL username password");
    System.exit(-1);
    }
    urlString=args[0];
    username=args[1];
    password=args[2];
    Authenticator.setDefault(new MyAuthenticator(username,password));
    try{
    System.out.println(URLEncoder.encode(urlString,"UTF-8"));
    }catch(UnsupportedEncodingException e){
    System.err.println(e);
    }
    URL url=new URL(urlString);
    InputStream content=(InputStream)url.getContent();
    BufferedReader in=new BufferedReader(new InputStreamReader(content));
    String line;
    StringBuffer lineBuf=new StringBuffer("");
    //FileWriter out=new FileWriter(new File("F:\\URL.txt"));
    BufferedWriter out=new BufferedWriter(new OutputStreamWriter(new FileOutputStream(new File("F:\\URL.html"))));
    while((line=in.readLine())!=null){
    //System.out.println(line);
    lineBuf.append(line+"\n");
    }
    out.write(lineBuf.toString());
    out.flush();
    out.close(); System.out.println("Done.");
    }//end main static class MyAuthenticator extends Authenticator{
    private String username,password; public MyAuthenticator(String username,String password){
    this.username=username;
    this.password=password;
    } protected PasswordAuthentication getPasswordAuthentication(){
    System.out.println("Requesting Host     :"+getRequestingHost());
    System.out.println("Requesting Port     :"+getRequestingPort());
    System.out.println("Requesting Prompt   :"+getRequestingPrompt());
    System.out.println("Requesting Protocol :"+getRequestingProtocol());
    System.out.println("Requesting Scheme   :"+getRequestingScheme());
    System.out.println("Requesting Site     :"+getRequestingSite()); return new PasswordAuthentication(username,password.toCharArray());
    }
    }//end MyAuthenticator
    }//end testwhile这是一个可执行程序。需要参数用户名和密码的!
    java textwhile http://www.sina.com.cn username password
      

  2.   

    System.getProperties().put( "proxySet", "true" );System.getProperties().put( "proxyHost", "代理地址" );System.getProperties().put( "proxyPort", "代理端口" );连接的时候才需要指定用户与密码:URLConnection connection = url.openConnection();String password = "username:password";String encodedPassword = base64Encode( password );最好把相关信息写到配置文件或者jndi,方便移植connection.setRequestProperty( "Proxy-Authorization", encodedPassword );
      

  3.   

    请问bromon:base64Encode( password )方法如何实现? 或者是哪个类的方法? 你的程序编译通不过的。