您的位置:首页 > 编程语言 > Java开发

Java 读取网页简易程序

2016-05-12 09:17 375 查看
package com.ibm.downloadtool;

import java.io.BufferedInputStream;

import java.net.HttpURLConnection;

import java.net.URL;

import java.util.Scanner;

public class DownLoadTool {

public static void main(String[] args) {

// TODO Auto-generated method stub

DownLoadTool d = new DownLoadTool();

d.downLoadUrl("http://finance.sina.com.cn/");

}

//download

public String downLoadUrl(String addr){

StringBuffer sb=new StringBuffer();

try {

URL url=new URL(addr);

HttpURLConnection con= (HttpURLConnection) url.openConnection();

con.setConnectTimeout(1000);

con.connect();

System.out.println(con.getResponseCode());

if(con.getResponseCode()==200){

BufferedInputStream bis=new BufferedInputStream(con.getInputStream());

Scanner sc=new Scanner(bis,"GBK");

while(sc.hasNextLine()){

sb.append(sc.nextLine());

}

sc.close();

}

} catch (Exception e) {

e.printStackTrace();

}

System.out.println(sb.toString());

return sb.toString();

}

}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: