/**
  * 从HttpURLConnection对象读取指定字符,如果不匹配则抛出异常
  *
  * @param connection
  * @throws Exception
  */
  private static void checkConnectionContent(HttpURLConnection connection) throws Exception {
  try (InputStream inputStream = connection.getInputStream()) {
  byte[] b = new byte[1024];
  boolean success = false;
  StringBuffer sb = new StringBuffer();
  for (int i = inputStream.read(b); i > 0; i = inputStream.read(b)) {
  String tempStr = new String(b, 0, i);
  sb.append(tempStr);
  if (tempStr.indexOf("3,file not found") >= 0) {// service固定返回这个字符串,如果service作了更改此处也应更改
  success = true;
  break;
  }
  }
  if (!success) {
  String str = sb.toString();
  if (str.length() > 3) {
  char char0 = str.charAt(0);
  char char1 = str.charAt(1);
  if (Utils.isNumber(char0 + "") && char1 == ',') {
  success = true;
  }
  }
  }
  if (!success) {
  throw new RuntimeException("result content does not meet expectations.");
  }
  } catch (Exception ex) {
  throw ex;
  }
  }
  /**
  * 测试文件下载接口9067端口
  *
  * @param networkBean
  * @throws Exception
  */
  public static void testURLConnection() throws Exception {
  HttpURLConnection connection = (HttpURLConnection) new URL(GeneralWebServiceAddress.FILE_DOWN_URL_ADD + "path=").openConnection();
  checkConnectionContent(connection);
  }
  /**
  * 测试代理服务器连接
  */
  private void testProxyConnection() {
  NetworkBean readNetworkBean = NetworkProxyTool.readNetworkBean();// 先获得正在使用的NetworkBean
  try {
  NetworkBean networkBean = createNetworkBean();// 根据用户填写的信息创建的NetworkBean对象
  showTestResultDialog(NetworkProxyTool.testNetworkProxyBySystem(networkBean));
  } catch (Exception ex) {
  showTestResultDialog(false);
  }
  NetworkProxyTool.setNetworkProxyBySystem(readNetworkBean);// 测试结束,还原原来的NetworkBean
  }
  5.因为java连接网络时,如果使用当前的代理连接失败,那么会使用操作系统中缓存的代理进行网络连接,如何是测试连接时不使用操作系统缓存,但测试结束后使用操作系统缓存。
  /**
  * 设置长连接和验证信息缓存是否开启
  *
  * @param keepAlive
  */
  public static void setKeepAliveAndAuthCache(boolean keepAlive) {
  System.setProperty("http.keepAlive", keepAlive + "");// 设置是否开始长连接,如果为false可以防止连接被缓存(如果连接被缓存,用户名密码等所有信息都会被缓存)
  if (keepAlive) {
  AuthCacheValue.setAuthCache(new AuthCacheImpl());
  } else {
  // 设置一个空的实现AuthCache可以防止用户名密码信息被缓存
  AuthCacheValue.setAuthCache(new AuthCache() {
  public void remove(String pkey, AuthCacheValue entry) {
  }
  public void put(String pkey, AuthCacheValue value) {
  }
  public AuthCacheValue get(String pkey, String skey) {
  return null;
  }
  });
  }
  }
  在登录之前:
  NetworkProxyTool.setKeepAliveAndAuthCache(false);
  new.Login();
  登录成功后:
  NetworkProxyTool.setKeepAliveAndAuthCache(true);
  6.保存用户的代理配置:
  因为直接序列化自定义类型的对象,会存在版本问题(比如这个类的包名、类名改了,死定了)。
  所以不能直接序列化自定义类的对象,二是将自定义类对象的属性转成字典(Map),然后序列化map。
  /**
  * 读取本地网络代理设置配置文件
  *
  * @return
  */
  public static NetworkBean readNetworkBean() {
  NetworkBean networkBean = getCurrNetworkBean(readNetworkBeanMap());
  if (networkBean == null) {
  networkBean = new NetworkBean();
  networkBean.setType(Proxy.Type.DIRECT);
  }
  return networkBean;
  }
  /**
  * 获取用户上次选择网络代理设置
  *
  * @param map
  * @return
  */
  public static NetworkBean getCurrNetworkBean(Map map) {
  putBrowserProxy(map);
  return (NetworkBean) map.get(getTypeMapKey(map));
  }
  /**
  * 将浏览器的信息存放入代理信息总配置map
  *
  * @param map
  */
  private static void putBrowserProxy(Map map) {
  if (browserProxyBean == null) {
  browserProxyBean = getBrowserProxy();
  }
  NetworkBean networkBeanBrowser = (NetworkBean) map.get(key_browser);
  if (networkBeanBrowser == null) {
  networkBeanBrowser = browserProxyBean;
  }
  if ((Utils.toString(browserProxyBean.getAddress()).isEmpty() || !browserProxyBean.getAddress().equals(networkBeanBrowser.getAddress()))
  || (Utils.toString(browserProxyBean.getPort()).isEmpty() || !browserProxyBean.getPort().equals(networkBeanBrowser.getPort()))) {
  networkBeanBrowser.setUsername(null);
  networkBeanBrowser.setPassword(null);
  networkBeanBrowser.setDomain(null);
  }
  networkBeanBrowser.setType(browserProxyBean.getType());
  networkBeanBrowser.setTypeText(browserProxyBean.getTypeText());
  networkBeanBrowser.setAddress(browserProxyBean.getAddress());
  networkBeanBrowser.setPort(browserProxyBean.getPort());
  map.put(key_browser, networkBeanBrowser);
  }
  在登录之前:
  NetworkBean networkBean = NetworkProxyTool.readNetworkBean();
  NetworkProxyTool.setNetworkProxyBySystem(networkBean);
  NetworkProxyTool.setKeepAliveAndAuthCache(false);
  new.Login();
  在用户配置完代理点击确定时:
  /**
  * 点击确定
  */
  private void confirm() {
  if ((isHttp() || isSocks()) && !checkIpAndPortNotNull()) {
  return;
  }
  NetworkBean networkBean = createNetworkBean();
  if (isHttp()) {// HTTP代理
  if (networkBean.getDomain() != null) {
  networkBean.setDomain(networkBean.getDomain().trim());
  }
  proxySettingMap.put(key_http, networkBean);
  proxySettingMap.put(key_proxy_type, key_http);
  } else if (isSocks()) {// SOCKS代理
  proxySettingMap.put(key_socks, networkBean);
  proxySettingMap.put(key_proxy_type, key_socks);
  } else if (isBrowser()) {
  proxySettingMap.put(key_browser, networkBean);
  proxySettingMap.put(key_proxy_type, key_browser);
  } else {
  proxySettingMap.put(key_direct, networkBean);
  proxySettingMap.put(key_proxy_type, key_direct);
  }
  userCurrShowNetworkBean = networkBean;
  isConfirm = true;
  setVisible(false);
  NetworkProxyTool.saveNetworkProxyConfig(proxySettingMap);
  }