获取网页源代码

 

package webcon;

import java.io.BufferedReader;

public class WebCon {

	// 获取网页源代码
	public String getWebCon(String pageURL,String encoding) {
		// 
		StringBuffer sb = new StringBuffer();
		try {
			URL url = new URL(pageURL);
			BufferedReader in = new BufferedReader(new InputStreamReader(url
					.openStream(),encoding));
			String line;
			while ((line = in.readLine()) != null) {
				sb.append(line);
			}
			in.close();
		} catch (Exception e) { // Report any errors that arise
			sb.append(e.toString());
			System.err.println(e);
			System.err
					.println("Usage:   java   HttpClient   <URL>   [<filename>]");
		}
		return sb.toString();
	}

}

你可能感兴趣的:(java)