java网络编程工具-URLConnection

/**
 * <p>
 * 网络编程相关工具
 * </p>
 * 
 * @author 陈淦森
 * @version 1.0.1
 * @date 2016年2月23日
 */
public class NetworkUtils {
	
	/**
	 * 使用Java网络编程工具,模仿浏览器的首部发送HTTP请求,返回一个代表响应内容的数据流。
	 */
	public static InputStream getURLInputStream(String requestURL) throws IOException {
		URL u = new URL(requestURL);
		URLConnection urlConn = u.openConnection();
		urlConn.setRequestProperty("Host", u.getHost());
		urlConn.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0");
		urlConn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		urlConn.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
		urlConn.setRequestProperty("Accept-Encoding", "gzip, deflate");
		urlConn.setRequestProperty("Connection", "keep-alive");
		urlConn.setRequestProperty("Cache-Control", "max-age=0");
		String contentEncoding = urlConn.getContentEncoding();
		InputStream is = null;
		if (contentEncoding != null && contentEncoding.toLowerCase().contains("gzip")) {
			is = new GZIPInputStream(urlConn.getInputStream());
		} else {
			is = urlConn.getInputStream();
		}
		return is;
	}
	
	/**
	 * 使用Java网络编程工具,模仿浏览器的首部发送HTTP请求,返回一个URLResult对象。它包含响应的真实请求地址(如302跳转后的URL地址)
	 * 和代表响应内容的数据流
	 */
	public static URLResult getURLResult(String requestURL) throws IOException {
		URL u = new URL(requestURL);
		URLConnection uc = u.openConnection();
		uc.setRequestProperty("Host", u.getHost());
		uc.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:44.0) Gecko/20100101 Firefox/44.0");
		uc.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
		uc.setRequestProperty("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
		uc.setRequestProperty("Accept-Encoding", "gzip, deflate");
		uc.setRequestProperty("Connection", "keep-alive");
		uc.setRequestProperty("Cache-Control", "max-age=0");
		URLResult ur = new URLResult();
		String contentEncoding = uc.getContentEncoding();
		InputStream is = null;
		if (contentEncoding != null && contentEncoding.toLowerCase().contains("gzip")) {
			is = new GZIPInputStream(uc.getInputStream());
		} else {
			is = uc.getInputStream();
		}
		ur.setInputStream(is);
		ur.setRealPath(uc.getURL().getPath());
		return ur;
	}
	
}

猜你喜欢

转载自blog.csdn.net/cgs666/article/details/50848437
今日推荐