Java查找代理服務器設置
如何找到係統的代理設置?
解決方法
下麵的例子展示了如何使用HttpURLConnection類的systemSetting&GetResponse方法查找代理服務器設置和創建,把係統上的代理連接。
import java.net.HttpURLConnection; import java.net.URL; import java.util.Properties; import java.net.InetSocketAddress; import java.net.Proxy; import java.net.ProxySelector; import java.net.URI; public class Main{ public static void main(String s[]) throws Exception{ try { Properties systemSettings = System.getProperties(); systemSettings.put("proxySet", "true"); systemSettings.put("http.proxyHost", "proxy.mycompany1.local"); systemSettings.put("http.proxyPort", "80"); URL u = new URL("http://www.google.com"); HttpURLConnection con = (HttpURLConnection) u.openConnection(); System.out.println(con.getResponseCode() + " : " + con.getResponseMessage()); System.out.println(con.getResponseCode() == HttpURLConnection.HTTP_OK); } catch (Exception e) { e.printStackTrace(); System.out.println(false); } System.setProperty("java.net.useSystemProxies", "true"); Proxy proxy = (Proxy) ProxySelector.getDefault(). select(new URI("http://www.yahoo.com/")).iterator(). next();; System.out.println("proxy hostname : " + proxy.type()); InetSocketAddress addr = (InetSocketAddress) proxy.address(); if (addr == null) { System.out.println("No Proxy"); } else { System.out.println("proxy hostname : " + addr.getHostName()); System.out.println("proxy port : " + addr.getPort()); } } }
結果
上麵的代碼示例將產生以下結果。
200 : OK true proxy hostname : HTTP proxy hostname : proxy.mycompany1.local proxy port : 80