Java获取网页源代码

Java获取网页源码

话不多说直接上代码吧

用Java获取一个网页的源码会经常使用到。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import 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"));
}
}

代码很简单‘巴特’,会很常用,做一个记录。