注意:利用URL发送的请求,服务器只返回实体部分,不包括http信息头部分的内容.
package cn.itcast.httpserver; import java.io.BufferedReader; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; public class TestURLConnection { public static void main(String[] args) throws Exception { URL url = new URL("http://127.0.0.1/index.html"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); //connection.getInputStream() 调用该方法才正在意义上去取数据 BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); String s =null; while((s=reader.readLine())!=null){ System.out.println(s); } reader.close(); connection.disconnect(); } }