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; } }
相关文章
最新发布
阅读排行
热门文章
猜你喜欢