Java获取网页源代码 发表于 2017-03-31 | 分类于 java Java获取网页源码话不多说直接上代码吧用Java获取一个网页的源码会经常使用到。 12345678910111213141516171819202122232425import java.io.BufferedInputStream;import java.net.URL;/** * Created by bighandsome on 2017/3/31. */public class HttpTest { public static String getWebCon(String pageURL) { String content = new String(); byte[] buf = new byte[1024]; try { URL url = new URL(pageURL); BufferedInputStream gzip_in = new BufferedInputStream(url.openStream()); while ((gzip_in.read(buf, 0, buf.length)) != -1) { content = content + (new String(buf, "gb2312"));//gb2312根据实际情况可以转换大多为UTF-8 } } catch (Exception e) { e.printStackTrace(); } return content; } public static void main(String args[]){ System.out.println(getWebCon("http://www.biquge.la/book/5552/3196902.html")); }} 代码很简单‘巴特’,会很常用,做一个记录。