URL openURL = null;
openURL = new URL("http://www.qq.com");
URLConnection con = null;
HttpURLConnection httpcon = null;
con = openURL.openConnection();
Properties prop = System.getProperties();
prop.put("proxySet","true");
prop.setProperty("http.proxyHost", "20,32,11,23");
prop.setProperty("http.proxyPort", "8023");
httpcon = (HttpURLConnection)con;
httpcon.disconnect();
httpcon.addRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
httpcon.setConnectTimeout(3000);
httpcon.connect();
StringBuilder sb = new StringBuilder();
String inputLine;
BufferedReader in = null;
try{
in = new BufferedReader(new InputStreamReader(httpcon.getInputStream(), "gb2312"));
}catch(java.net.SocketException e){
throw e;
}
while ((inputLine = in.readLine()) != null) {
sb.append(inputLine).append("\r\n");
}
in.close();
httpcon.disconnect();
System.out.println(sb);
注意:在这里的代理IP和端口号都是本人随便写的,是没用的一上IP,但是我运行中确能通过这样连接到网络并获取到网页的数据!
郁闷呀!谁能帮我解释下....为什么会这样?
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("","".toCharArray());
}
});
一个英文的,一个翻译的:
http://apicode.gicp.net/class.do?api=selectByfatherIndex&father=255
http://apicodecn.gicp.net/class.do?api=selectByfatherIndex&father=255
InetSocketAddress addr = new InetSocketAddress("218.14.227.197",3128);
Proxy proxy = new Proxy(Proxy.Type.HTTP, addr);
URL url = new URL("http://www.baidu.com");
URLConnection con = url.openConnection(proxy);
这样可以避免 此情况
在之前设置System.getProperties().setProperty("proxySet", "true");
System.getProperties().setProperty("http.proxyHost", "");
System.getProperties().setProperty("http.proxyPort", "");
所以没去用,
回8楼:我使用的都是免费代理,所以不使用用户名和密码,现在问题已经解决:
唉,已经有这么多高手做出了那么多的好用开源工具了,为什么我总是要自己去写呢!
最后使用apache中的一个开源包:commons-httpclient开做对网络的代理访问,问题解决...