Java设置代理
1.可以配置jvm虚拟机代理
java -Dhttp.proxyHost=192.168.0.1 -Dhttp.proxyPort=80 MyJavaApp
2.配置系统代理
Properties prop = System.getProperties();
prop.put("http.proxyHost","192.168.0.1");
prop.put("http.proxyPort","80");
或
System.setProperty("proxyType", "4");
System.setProperty("proxyPort", "80"));
System.setProperty("proxyHost", "127.0.0.1");
System.setProperty("proxySet", "true");
3.配置URLConnection代理
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("123.0.0.1", 8080));
URL url = new URL("http://www.yahoo.com");
HttpURLConnection uc = (HttpURLConnection)url.openConnection(proxy);
uc.connect();
String page;
StringBuffer tmp = new StringBuffer();
BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream()));
while ((line = in.readLine()) != null){
page.append(line + "\n");
}
System.out.println(page);
第二种用过其它两种没有用,感觉第一种更灵活一些