java网络编程:获取某个网址的html内容

时间:2021-08-06 15:02:09

 HtmlRequest类的内容:

package com.capinfotech.net;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class HtmlRequest {

		public static void main(String[] args) throws IOException {
			URL url = new URL("http://www.163.com/");
			HttpURLConnection conn = (HttpURLConnection)url.openConnection();
			InputStream inputStream = conn.getInputStream();   //通过输入流获得网站数据
			byte[] getData = readInputStream(inputStream);     //获得网站的二进制数据
			String data = new String(getData, "gb2312");
			System.out.println(data);
			
		}

		public static  byte[] readInputStream(InputStream inputStream) throws IOException {
			byte[] buffer = new byte[1024];
			int len = 0;
			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			while((len = inputStream.read(buffer)) != -1) {
				bos.write(buffer, 0, len);
			}
			
			bos.close();
			return bos.toByteArray();
		}
		
}

这样就能获得http://www.163.com的内容,在控制台会打印输出