首页 > 编程语言 > 详细

java 获取网页指定内容

时间:2015-11-22 15:53:12      阅读:245      评论:0      收藏:0      [点我收藏+]
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class HttpTest {
 String urlString;
 public static void main(String[] args) throws Exception {
  HttpTest client = new HttpTest("http://www.baidu.com");
  client.run();
 }
 public HttpTest(String urlString) {
  this.urlString = urlString;
 }
 public void run() throws Exception {
  //生成一个URL对象 
  URL url = new URL(urlString);
  //打开URL 
  HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
  //得到输入流,即获得了网页的内容 
  BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection
    .getInputStream()));
  String line;
  // 读取输入流的数据,并显示
  while ((line = reader.readLine()) != null){
   System.out.println(line);
  }
 }
}

 

java 获取网页指定内容

原文:http://www.cnblogs.com/hellowzd/p/4985923.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!