post方式的数据抓取

 1   public static String sendPost(String url, String param) {
 2         PrintWriter out = null;
 3         BufferedReader in = null;
 4         String result = "";
 5         try {
 6             URL realUrl = new URL(url);
 7             // 打开和URL之间的连接
 8             URLConnection conn = realUrl.openConnection();
 9             // 设置通用的请求属性
10             conn.setRequestProperty("accept", "*/*");
11             conn.setRequestProperty("connection", "Keep-Alive");
12             conn.setRequestProperty("user-agent",
13                     "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
14             // 发送POST请求必须设置如下两行
15             conn.setDoOutput(true);
16             conn.setDoInput(true);
17             
18             // 获取URLConnection对象对应的输出流
19             out = new PrintWriter(conn.getOutputStream());
20             // 发送请求参数
21             out.print(param);
22             // flush输出流的缓冲
23             out.flush();
24             // 定义BufferedReader输入流来读取URL的响应
25             in = new BufferedReader(
26                     new InputStreamReader(conn.getInputStream()));
27             String line;
28             while ((line = in.readLine()) != null) {
29                 result += line;
30             }
31         } catch (Exception e) {
32             System.out.println("发送 POST 请求出现异常!"+e);
33             e.printStackTrace();
34         }
35         //使用finally块来关闭输出流、输入流
36         finally{
37             try{
38                 if(out!=null){
39                     out.close();
40                 }
41                 if(in!=null){
42                     in.close();
43                 }
44             }
45             catch(IOException ex){
46                 ex.printStackTrace();
47             }
48         }
49         return result;
50     }   
51    

 

posted @ 2017-06-21 11:08  啄木鸟伍迪  阅读(1375)  评论(0编辑  收藏  举报
//火箭 GenerateContentList();