根据URL获取网页源码

根据给定的URL获取源码,同时还需要页面的Encoding
public String getPageSource() {
		StringBuffer sb = new StringBuffer();
		try {
			//构建一URL对象
			URL url = new URL(pageUrl);
			//使用openStream得到一输入流并由此构造一个BufferedReader对象
			BufferedReader in = new BufferedReader(new InputStreamReader(url
					.openStream(), encoding));
			String line;
			//读取www资源
			while ((line = in.readLine()) != null) {
				sb.append(line);
				sb.append("\n");
			}
			in.close();
		} catch (Exception ex) {
			sb.append(ex.toString());
			System.err.println(ex);
		}
		return sb.toString();
	}

你可能感兴趣的:(url)