Java写一个得到页面html代码

| 阅读数:--次| 作者:小豆豆
摘要: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;
	}
}
返回顶部
学到老代码浏览 关闭浏览