一、界面上要有这样几种代理类型可以选。
  1.HTTP代理
  2.Socks代理
  3.不使用代理(直连)
  4.使用浏览器设置(浏览器也是HTTP、Socks、直连三种)。
  可参考QQ登录设置里的代理能,其实跟qq的代理功能是一样的。
  二、测试使用所填写的代理配置信息是否可连接
  三、记录用户上次选择的代理配置,默认使用用户上次使用的代理配置进行网络连接。

  程序运行环境是WindowsXP、Windows7、Windows8系统。
  使用的技术为Java7,Swing,CXF。
  难点:
  1.如何进行全居的代理设置:
  /**
  * 网络代理Bean
  *
  * @author tang
  */
  public class NetworkBean implements Serializable {
  private static final long serialVersionUID = 1L;
  // private static sun.misc.BASE64Encoder base64Encoder = new sun.misc.BASE64Encoder();
  private Proxy.Type type;// 代理类型
  private String address;// ip 地址
  private String port;// 端口号
  private String username;// 代理服务器用户名
  private String password;// 代理服务器用户密码
  private String domain;// 域
  private String typeText;// 代理类型显示的文本
  public NetworkBean() {
  }
  public NetworkBean(Type type, String address, String port, String username, String password) {
  this.type = type;
  this.address = address;
  this.port = port;
  this.username = username;
  this.password = password;
  }
  public NetworkBean(Type type, String address, String port, String username, String password, String domain) {
  super();
  this.type = type;
  this.address = address;
  this.port = port;
  this.username = username;
  this.password = password;
  this.domain = domain;
  }
  public Proxy.Type getType() {
  return type;
  }
  public void setType(Proxy.Type type) {
  this.type = type;
  }
  public String getAddress() {
  return address;
  }
  public void setAddress(String address) {
  this.address = address;
  }
  public String getPort() {
  return port;
  }
  public void setPort(String port) {
  this.port = port;
  }
  public String getUsername() {
  return username;
  }
  public void setUsername(String username) {
  this.username = username;
  }
  public String getPassword() {
  return password;
  }
  public void setPassword(String password) {
  this.password = password;
  }
  public String getDomain() {
  return domain;
  }
  public void setDomain(String domain) {
  this.domain = domain;
  }
  public String getTypeText() {
  return typeText;
  }
  public void setTypeText(String typeText) {
  this.typeText = typeText;
  }
  /**
  * return domain + "" + username
  */
  public String getDomainAndUsername() {
  return (Utils.toString(domain).trim().isEmpty()) ? username : domain.trim() + "\" + username;
  }
  /**
  * return domain + "" + username + ":" + password
  */
  public String getDomainAndUsernameAndPassword() {
  return getDomainAndUsername() + ":" + password;
  }
  /**
  * return username + ":" + password
  */
  public String getUsernameAndPassword() {
  return username + ":" + password;
  }
  /**
  * return (domain + "" + username + ":" + password) to 64 bit
  */
  public String getDomainAndUsernameAndPassword64() {
  return org.apache.commons.codec.binary.Base64.encodeBase64String(getDomainAndUsernameAndPassword().getBytes());
  }
  @Override
  public String toString() {
  return "NetworkBean [type=" + type + ", typeText=" + typeText + ", address=" + address + ", port=" + port + ", username=" + username + ", password="
  + password + ", domain=" + domain + "]";
  }
  }
  /**
  * 根据指定的代理信息设置系统全局的网络代理
  *
  * @param networkBean
  */
  public static void setNetworkProxyBySystem(NetworkBean networkBean) {
  System.out.println("System Set Proxy : " + networkBean);
  if (isUserProxy(networkBean)) {
  if (networkBean.getType() == Proxy.Type.SOCKS) {
  System.getProperties().remove("http.proxyHost");
  System.getProperties().remove("http.proxyPort");
  System.getProperties().setProperty("socksProxyHost", networkBean.getAddress());
  System.getProperties().setProperty("socksProxyPort", networkBean.getPort());
  } else {
  System.getProperties().setProperty("http.proxyHost", networkBean.getAddress());
  System.getProperties().setProperty("http.proxyPort", networkBean.getPort());
  }
  Authenticator.setDefault(new BairuiAuthenticator(networkBean.getDomainAndUsername(), networkBean.getPassword()));
  } else if (networkBean != null) {
  System.getProperties().remove("proxySet");
  System.getProperties().remove("socksProxySet");
  System.getProperties().remove("http.proxyHost");
  System.getProperties().remove("http.proxyPort");
  System.getProperties().remove("socksProxyHost");
  System.getProperties().remove("socksProxyPort");
  }
  }
  /**
  * 网络用户名密码校验提供者
  */
  public static class BairuiAuthenticator extends Authenticator {
  private String username, password;
  public BairuiAuthenticator(String username, String password) {
  this.username = username;
  this.password = password;
  }
  protected PasswordAuthentication getPasswordAuthentication() {
  return new PasswordAuthentication(username, password == null ? null : password.toCharArray());
  }
  }
  2.如何让CXF的Service使用系统的代理:
  /**
  * 为WebService接口添加网络代理支持:httpClientPolicy.setAllowChunking(false);
  *
  * @param client
  */
  public static void setWebServiceSupportProxy(Client client) {
  HTTPConduit conduit = (HTTPConduit) client.getConduit();
  HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
  httpClientPolicy.setAllowChunking(false);
  conduit.setClient(httpClientPolicy);
  }
  3.使用浏览器设置:
  /**
  * 读取用户注册表获取浏览器的代理设置
  *
  * @return 该方法不会返回null值也不会抛出异常
  */
  public static NetworkBean getBrowserProxy() {
  NetworkBean networkBean = new NetworkBean();
  networkBean.setTypeText(typeTexts[3]);
  networkBean.setType(Proxy.Type.DIRECT);
  String key = "reg query "HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Internet Settings"";// 注册表浏览器代理key
  try {
  Process exec = Runtime.getRuntime().exec(key);
  try (InputStreamReader i = new InputStreamReader(exec.getInputStream()); BufferedReader ir = new BufferedReader(i)) {
  for (String line = ir.readLine(); line != null; line = ir.readLine()) {
  if (line.indexOf("ProxyServer") >= 0) {
  String[] split1 = line.split("    ");
  if (split1.length > 3) {
  String[] split2 = split1[3].trim().split(":");
  if (split2.length > 1) {
  if (!Utils.toString(split2[0]).isEmpty() && !Utils.toString(split2[1]).isEmpty()) {
  networkBean.setAddress(split2[0]);
  networkBean.setPort(split2[1]);
  networkBean.setType(Proxy.Type.HTTP);
  break;
  }
  }
  }
  }
  }
  } catch (Exception e) {
  e.printStackTrace();
  }
  } catch (Exception e) {// 从注册表读取失败
  e.printStackTrace();
  }
  return networkBean;
  }
  4.测试代理是否可用
  因为程序中使用了服务器的两个不同的端口,所以需要测试两个端口是否都可连。
  如果使用了多台服务器,更加需要分别测试了。
  /**
  * 测试网络代理是否能通过连接,如果不通过抛出异常
  *
  * @throws Exception
  */
  private static void testNetworkProxy() throws Exception {
  testWebService();
  testURLConnection();
  }
  /**
  * 测试CXF Service接口50367端口
  *
  * @param networkBean
  * @throws Exception
  */
  public static void testWebService() throws Exception {
  JcptLoginService service = WebServiceUtils.getService(JcptLoginService.class, GeneralWebServiceAddress.LOGIN_SERVICE_URL_ADD);
  String result = service.getLoginPicture();
  System.out.println(result);
  }