HttpURLConnection设置代理服务器

1、设置系统数据
java  -Dhttp.proxyHost=proxy.com  -Dhttp.proxyPort=80 MyJavaApp
或者
System.setProperty("http.proxyHost", "proxy.com");
System.setProperty("http.proxyPort", "80");
其他可设置的属性
http://docs.oracle.com/javase/6/docs/technotes/guides/net/properties.html

从1.5起可以通过Proxy类来打开链接
Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("176.93.133.144", 8080));
URL url = new URL("http://www.google.com.hk");
HttpURLConnection uc = (HttpURLConnection)url.openConnection(proxy);
uc.connect();
设置代理服务器的用户名和密码
System.setProperty("http.proxyHost", "proxy.com");
System.setProperty("http.proxyPort", "80");
URL url=new URL("http://someserver/somepage");
URLConnection uc = url.openConnection ();
String encoded = new String
      (Base64.base64Encode(new String("username:password").getBytes()));
uc.setRequestProperty("Proxy-Authorization", "Basic " + encoded);
uc.connect();

你可能感兴趣的:(java,String,服务器,url,basic)