当前位置: 代码迷 >> Web前端 >> java-用HttpURLConnection发送Http请求
  详细解决方案

java-用HttpURLConnection发送Http请求

热度:1082   发布时间:2012-11-25 11:44:31.0
java-用HttpURLConnection发送Http请求.
注意:利用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();
	}
	
}
  相关解决方案