swing 桌面客户端系统设置网络代理

用swing开发的一个桌面客户端系统考虑到有的系统用户网络会采用代理服务器。于是要提供代理设置功能。

 

 


swing 桌面客户端系统设置网络代理

 

这里可以通过选择是HTTP代理 或者 SOCKS5 代理。

 

 

String proxyType = prop.getProperty("proxyType");
			
			if("HTTP".equals(proxyType)){
				
				String proxyHost = prop.getProperty("proxyHost");
				String proxyPort = prop.getProperty("proxyPort");
				String proxyUsername = prop.getProperty("proxyUsername");
				String proxyPassword = prop.getProperty("proxyPassword");
				String proxyDomain = prop.getProperty("proxyDomain");
	            System.setProperty("http.proxyHost", proxyHost);
	            System.setProperty("http.proxyPort", proxyPort);
	            System.setProperty("https.proxyHost", proxyHost);
	            System.setProperty("https.proxyPort", proxyPort);
	            Authenticator.setDefault(new MyAuthenticator(proxyUsername, proxyPassword));
	            System.setProperty("http.proxySet", "true");
	            
			}
			if("SOCKS".equals(proxyType)){
				String proxyHost_socks = prop.getProperty("proxyHost_socks");
				String proxyPort_socks = prop.getProperty("proxyPort_socks");
				String proxyUsername_socks = prop.getProperty("proxyUsername_socks");
				String proxyPassword_socks = prop.getProperty("proxyPassword_socks");
				String proxyDomain_socks = prop.getProperty("proxyDomain_socks");
				System.setProperty("socksProxyHost", proxyHost_socks);
		        System.setProperty("socksProxyPort", proxyPort_socks);
		        
		        Authenticator.setDefault(new MyAuthenticator(proxyUsername_socks, proxyPassword_socks));
		        
			}
 

 

 

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

 

这里是主要代码,用户填写的信息保存在配置文件中,环境测试 我用的ccproxy 来实现代理服务

 

你可能感兴趣的:(swing)