代理服务器访问网页内容代码

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.Properties;

public class ProxyTest {
public static void main(String[] args) {
String strUrl = "http://roll.news.sina.com.cn/news/gnxw/gdxw1/index.shtml";
URL url;
URLConnection conn;
try {
url = new URL(strUrl);
conn = url.openConnection();

conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");
String strProxy = "172.20.1.2";
String strPort = "80";
Properties systemProperties = System.getProperties();
systemProperties.setProperty("http.proxyHost", strProxy);
systemProperties.setProperty("http.proxyPort", strPort);
BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream(), "GBK"));
String ss;
while ((ss = rd.readLine()) != null) {
System.out.println(ss);
}
rd.close();
} catch (Exception e) {
e.printStackTrace();
System.err.println(e);
}

}
}

猜你喜欢

转载自jackaney.iteye.com/blog/1153466