首页 > Java

Java写一个得到页面html代码

发表于2014-09-05 11:21:33| --次阅读| 来源webkfa| 作者小豆豆

摘要:Java写一个得到页面html代码
java代码
package com.webkfa.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Test {
	
	public static void main(String[] args) throws IOException{
		 StringBuffer htmlbf = getHtmlContext("http://www.webkfa.com");
		System.out.println(htmlbf.toString());
	}
	/**
	 * 得到http内容
	 * @param hpath
	 * @return
	 * @throws IOException
	 */
	public static StringBuffer getHtmlContext(String hpath) throws IOException{
		StringBuffer bf=new StringBuffer();
		HttpURLConnection httpUrl = null;
		URL uobj = new URL(hpath);
		httpUrl = (HttpURLConnection) uobj.openConnection();
		httpUrl.connect();
		InputStream is = httpUrl.getInputStream();
		InputStreamReader isr = new InputStreamReader(is);
		BufferedReader br = new BufferedReader(isr);
		String line = null;
		while( (line = br.readLine()) != null ){
			bf.append(line+"\r\n");
		}
		br.close();
		isr.close();
		is.close();
		
		return bf;
	}
}

相关文章

猜你喜欢

学到老在线代码浏览器 关闭浏览
友情链接: hao123 360导航 搜狗网址导航 114啦网址导航 博客大全
Copyright © 1999-2014, WEBKFA.COM, All Rights Reserved  京ICP备14034497号-1