现在的位置: 首页 > 综合 > 正文

用java 获取指定url 网站内容

2013年03月25日 ⁄ 综合 ⁄ 共 1051字 ⁄ 字号 评论关闭

package com.util;

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");

 }

}
 

抱歉!评论已关闭.