java获取系统的代理配置。
当系统设置了全局代理的时候,咱们的java应用是不会走系统代理的,需要自己获取系统代理,然后将java设置成代理了。其实系统代理是在window系统的注册表中,能获取到注册表的值,其实就是能获取到系统的代理配置。下面的代码,是能获取到系统的注册表的值。window系统的代理配置是在注册表的位置是HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings。
package net.mn886.fiddler.tools; public class ProxyModel { private boolean isOpenProxy = false; private String ip; private int port; public boolean isOpenProxy() { return isOpenProxy; } public void setOpenProxy(boolean isOpenProxy) { this.isOpenProxy = isOpenProxy; } public String getIp() { return ip; } public void setIp(String ip) { this.ip = ip; } public int getPort() { return port; } public void setPort(int port) { this.port = port; } @Override public String toString() { return "ProxyModel [isOpenProxy=" + isOpenProxy + ", ip=" + ip + ", port=" + port + "]"; } }
=========================
package net.mn886.fiddler.tools; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.util.HashMap; import java.util.Map; public class Regedit { public static Map<String,String> queryRegPath(String path) throws IOException{ Map<String,String> ret = new HashMap<String,String>(); Process ps = null; ps = Runtime.getRuntime().exec("reg query \""+path+"\""); ps.getOutputStream().close(); InputStreamReader i = new InputStreamReader(ps.getInputStream()); String line; BufferedReader ir = new BufferedReader(i); while ((line = ir.readLine()) != null) { if(line.startsWith(path) && !line.equals(path)){ ret.put(line.trim(),null); } } return ret; } public static Map<String,String> queryRegValue(String path) throws IOException{ Map<String,String> ret = new HashMap<String,String>(); Process ps = null; ps = Runtime.getRuntime().exec("reg query \""+path+"\""); ps.getOutputStream().close(); InputStreamReader i = new InputStreamReader(ps.getInputStream()); String line; BufferedReader ir = new BufferedReader(i); boolean isDefault = true; while ((line = ir.readLine()) != null) { if(line.indexOf("REG_SZ")>0){ String[] keyvalue = line.split("REG_SZ"); if(isDefault){ ret.put("default", keyvalue[1].trim()); isDefault = false; }else{ ret.put( keyvalue[0].trim(), keyvalue[1].trim()); } }else if(line.indexOf("REG_DWORD")>0){ String[] keyvalue = line.split("REG_DWORD"); if(isDefault){ ret.put("default", keyvalue[1].trim()); isDefault = false; }else{ ret.put( keyvalue[0].trim(), keyvalue[1].trim()); } } } return ret; } }
===================
package net.mn886.fiddler.tools; import java.io.IOException; import java.util.Map; public class ProxyServer { public static ProxyModel getSysProxy(){ Map<String, String> dd = null; ProxyModel proxyModel = new ProxyModel(); try { dd = Regedit.queryRegValue("HKEY_CURRENT_USER\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"); String proxyEnable = dd.get("ProxyEnable"); if(proxyEnable!=null && "0x1".equals(proxyEnable)){ String proxyServer = dd.get("ProxyServer"); if(proxyServer!=null && !"".equals(proxyServer)){ String p[] = proxyServer.split(":"); if(p.length>=2){ proxyModel.setIp(p[0]); proxyModel.setPort(Integer.parseInt(p[1])); }else{ proxyModel.setIp(p[0]); proxyModel.setPort(80); } proxyModel.setOpenProxy(true); } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return proxyModel; } }
-----------------------------------------------------
转载请注明来源此处
原地址:#
发表