ITeye博客

java 程序代理设置

liulang203 · 2011-03-21 10:33 · 编程语言 · HTML · Java · .net · Oracle · Markdown

		URL url;
		System.setProperty("http.proxyHost", "192.168.1.1");
		System.setProperty("http.proxyPort", "3128");
		Authenticator.setDefault(new ProxyAuthenticator("name", "password"));

		try {
			url = new URL("http://download.oracle.com/javase/6/docs/technotes/guides/net/proxies.html");
			InputStream in = url.openStream();
			int i  = -1;
			while((i=in.read())>-1){
				System.out.print((char)i);
			}
			in.close();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

ProxyAuthenticator.java



import java.net.Authenticator;
import java.net.PasswordAuthentication;

public class ProxyAuthenticator extends Authenticator {
    private String userName, password; 
    protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(userName, password.toCharArray()); 
    } 
    public ProxyAuthenticator(String userName, String password) { 
        this.userName = userName; 
        this.password = password; 
    } 
}

参考资料:http://download.oracle.com/javase/6/docs/technotes/guides/net/proxies.html

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。