/**
*
* (通过网站域名URL获取该网站的源码)<BR>
* 方法名:getURLSource<BR>
* @author Allister.Liu(刘继鹏) <BR>
* Date:2016年9月12日-上午11:01:29 <BR>
* @param urls
* @return
* @throws Exception String<BR>
* @exception <BR>
* @since 1.0.0
*/
public static String getURLSource(String urls) throws Exception {
URL url = new URL(urls);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5 * 1000);
InputStream inStream = conn.getInputStream(); // 通过输入流获取html二进制数据
byte[] data = readInputStream(inStream); // 把二进制数据转化为byte字节数据
String htmlSource = new String(data);
return htmlSource;
}
/**
*
* (把二进制流转化为byte字节数组)<BR>
* 方法名:readInputStream<BR>
* @author Allister.Liu(刘继鹏) <BR>
* Date:2016年9月12日-上午10:55:58 <BR>
* @param instream
* @return
* @throws Exception byte[]<BR>
* @exception <BR>
* @since 1.0.0
*/
public static byte[] readInputStream(InputStream instream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1204];
int len = 0;
while((len = instream.read(buffer)) != -1){
outStream.write(buffer, 0, len);
}
instream.close();
return outStream.toByteArray();
}
public static void main(String[] args) throws Exception {
System.out.println(getURLSource("http://www.csdn.net"));
}