Home  >  Article  >  Web Front-end  >  How to get Html code using the Internet? Principle of obtaining Html code from the network

How to get Html code using the Internet? Principle of obtaining Html code from the network

零下一度
零下一度Original
2017-04-26 15:06:451437browse

How to obtain Html code using the Internet? The editor below brings an article on the principle of obtaining Html code from the Internet, for reference only

package cn.captain.html;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class htmlRequest {
		/**
		 * @param args
		 * @throws MalformedURLException 
		 */
		public static void main(String[] args) throws Exception
		{
			URL url = new URL("http://www.baidu.com/"); 
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			conn.setRequestMethod("GET");
			conn.setConnectTimeout(5 * 1000);
			InputStream inStream =  conn.getInputStream();//通过输入流获取html数据	
			byte[] data = readInputStream(inStream);//得到html的二进制数据
			String html = new String(data);
			System.out.println(html);
		}
	   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();     	
		}

	}

The above is the detailed content of How to get Html code using the Internet? Principle of obtaining Html code from the network. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn