java-HttpURLConnectionでHttp要求を送信します。

867 ワード

注意: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();
	}
	
}