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 ""; } } }
另参考: