悉野小楼

导航

java读取网页

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * java读取网页
 */
public class Main {

    public static void main(String[] args) {
        String ret = getWebString("http://www.baidu.com");
        System.out.print(ret);
        String ret2 = getWebString2("http://www.360.cn");
        System.out.print(ret2);
    }

    /**
     * 使用字节数组转成字符串
     * @param webUrl
     * @return
     */
    public static String getWebString(String webUrl) {
        try {
            URL url = new URL(webUrl);
            HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
            urlConnection.connect();
            int len = urlConnection.getContentLength();
            InputStream inputStream = urlConnection.getInputStream();
            byte[] bytes = new byte[len];
            inputStream.read(bytes, 0, len);
            inputStream.close();
            String strRet = new String(bytes);
            return strRet;
        }
        catch(Exception ex) {
            return "";
        }
    }

    /**
     * 使用BuffedReader读取
     * @param webUrl
     * @return
     */
    public static String getWebString2(String webUrl) {
        try {
            URL url = new URL(webUrl);
            InputStream inputStream = url.openStream();
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
            String line;
            StringBuilder sb = new StringBuilder();
            while((line = bufferedReader.readLine()) != null) {
                sb.append(line + "\r\n");
            }
            return sb.toString();
        }
        catch(Exception ex) {
            return "";
        }
    }
}

另参考:

c#使用Socket获取网页(含自动跳转, 解压网页)

 java使用socket读取网页

java读取网页源码下载

posted on 2020-06-01 20:24  悉野  阅读(659)  评论(0编辑  收藏  举报