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);
}
}
}
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);
}
}
}
本文提供了一个Java示例程序,展示如何通过设置HTTP代理来访问新浪新闻网站的一个具体页面。该程序首先定义了要访问的目标网址,并设置了HTTP请求头中的User-Agent字段,接着配置了代理服务器的主机名和端口,最后读取并打印了网页的内容。

被折叠的 条评论
为什么被折叠?



