Urlconnection 读取页面

package 

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
 * HttpConnection
 * @author 
 *
 */
public class HttpConnectionUtil {
	/**
	 * 通过url获取网络数据
	 * @param url 路径
	 * @return 字符串结果
	 */
	public static  String getInfoFromAddress(String url){
		StringBuffer sb=new StringBuffer();
		InputStream stream;
		String strLine = null;
		URL urL;
		try {
			urL = new java.net.URL(url);
			HttpURLConnection connection = (HttpURLConnection) urL.openConnection();
			connection.connect();
			stream = connection.getInputStream();
			BufferedReader reader = new BufferedReader(new InputStreamReader(stream,"utf-8")); 
			while ((strLine = reader.readLine()) != null) {
				sb.append(strLine);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return sb.toString();   
	}
}

 

你可能感兴趣的:(urlconnection)