码迷,mamicode.com
首页 > 编程语言 > 详细

Java 读取网页源代码

时间:2015-03-20 20:05:50      阅读:151      评论:0      收藏:0      [点我收藏+]

标签:

package com.sphere.service;

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 QueryService {

    /**
     * 发起http get请求获取网页源代码
     * @param requestUrl
     * @return
     * @throws IOException 
     */
    private static String httpRequest(String requestUrl) throws IOException{
        
        StringBuffer buffer = null;
        HttpURLConnection httpUrlConn = null;
        InputStream inputStream = null;
        InputStreamReader inputStreamReader = null;
        BufferedReader bufferedReader = null;
        
        try {
            // 建立连接  
            URL url = new URL(requestUrl);
            httpUrlConn = (HttpURLConnection)url.openConnection();
            httpUrlConn.setDoInput(true);  
            httpUrlConn.setRequestMethod("GET"); 
            
            //获取输入流
            inputStream = httpUrlConn.getInputStream();
            //    http://www.bjsubway.com/support/swzl/ 此网页是gb2312编码
            inputStreamReader  = new InputStreamReader(inputStream,"gb2312");
            bufferedReader = new BufferedReader(inputStreamReader);
            
            // 读取返回结果
           buffer = new StringBuffer();
           String str = null;
           while((str = bufferedReader.readLine())!= null){
               buffer.append(str);
           }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }finally{
            //释放资源,依次关闭流
            if(bufferedReader != null){
                bufferedReader.close();
            }
            if(inputStreamReader != null){
                inputStreamReader.close();
            }
            if(inputStream != null){
                inputStream.close();
            }
            //释放资源,关闭http连接
            if(httpUrlConn != null){
                httpUrlConn.disconnect();
            }
            
        }
        return buffer.toString();
    }
    
    public static void main(String[] args) throws IOException {
        String Url = "http://www.bjsubway.com/support/swzl/";
        String htmlString = httpRequest(Url);
        System.out.println(htmlString);
    }
}

 

Java 读取网页源代码

标签:

原文地址:http://www.cnblogs.com/sphere/p/4354296.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!