java获取网页源码

01 package gogo.cool;
02 
03 import java.io.BufferedReader;
04 import java.io.IOException;
05 import java.io.InputStreamReader;
06 import java.net.HttpURLConnection;
07 import java.net.URL;
08 
09 public class test1 {
10 
11     public static void main(String[] a) throws IOException {
12 
13         String url = "http://www.baidu.com";
14 
15         System.out.println(getHTML(url, "gbk")); // 使用原网页里声明的gb2312反而会出现乱码
16 
17     }
18 
19     public static String getHTML(String pageURL, String encoding) {
20 
21         StringBuilder pageHTML = new StringBuilder();
22 
23         try {
24 
25             URL url = new URL(pageURL);
26 
27             HttpURLConnection connection = (HttpURLConnection) url
28                     .openConnection();
29 
30             connection.setRequestProperty("User-Agent", "MSIE 7.0");
31 
32             BufferedReader br = new BufferedReader(new InputStreamReader(
33                     connection.getInputStream(), encoding));
34 
35             String line = null; 
36 
37             while ((line = br.readLine()) != null) {
38 
39                 pageHTML.append(line);
40 
41                 pageHTML.append("\r\n");
42 
43             }
44 
45             connection.disconnect();
46 
47         } catch (Exception e) {
48 
49             e.printStackTrace();
50 
51         }
52 
53         return pageHTML.toString();
54 
55     }
56 }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值