08-05
28
用java 获取指定url 网站内容
作者:Java伴侣 日期:2008-05-28
复制内容到剪贴板 程序代码
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
public class GetUrlContent {
public static String getUrlTheContent(String aimurl){
try{
URL url = new URL(aimurl);
//设置代理
//String proxy = "10.165.2.1:8080";
System.setProperty("proxySet","true");
System.setProperty("proxyHost","10.165.2.1");
System.setProperty("proxyPort","8080");
InputStream is = url.openStream();
BufferedReader bi = new BufferedReader(new InputStreamReader(is));
String str = "";
String returnstr="";
while ((str = bi.readLine()) != null) {
returnstr+=str;
System.out.println(str);
}
if(str==null)
str="没有获取到内容";
return returnstr;
}
catch(Exception e){
System.out.println("-----------Error----------"+e.getMessage());
return "发生了错误";
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
GetUrlContent.getUrlTheContent("http://www.sohu.com");
}
}
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
public class GetUrlContent {
public static String getUrlTheContent(String aimurl){
try{
URL url = new URL(aimurl);
//设置代理
//String proxy = "10.165.2.1:8080";
System.setProperty("proxySet","true");
System.setProperty("proxyHost","10.165.2.1");
System.setProperty("proxyPort","8080");
InputStream is = url.openStream();
BufferedReader bi = new BufferedReader(new InputStreamReader(is));
String str = "";
String returnstr="";
while ((str = bi.readLine()) != null) {
returnstr+=str;
System.out.println(str);
}
if(str==null)
str="没有获取到内容";
return returnstr;
}
catch(Exception e){
System.out.println("-----------Error----------"+e.getMessage());
return "发生了错误";
}
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
GetUrlContent.getUrlTheContent("http://www.sohu.com");
}
}
适用于采集
评论: 0 | 引用: 0 | 查看次数: 851
发表评论