Apache HttpClient FirstDemo

import java.io.InputStream;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;


/**
 * 
 * FileName HttpDemo.java  <br />
 * @author wenhao <br />
 * @version 1.0   <br />
 * @created 2013-1-10 下午7:02:08 <br />
 * 
 */

/**
 * 
 * @author wenhao <br />
 * @version 1.0   <br />
 * @created 2013-1-10 下午7:02:08 <br />
 * 
 */
public class HttpDemo {
    
    public static void main(String args[]) throws Exception{
        DefaultHttpClient http = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet("http://172.20.223.120/wenhao.txt");
        HttpResponse response = http.execute(httpGet);
        
        try {
            System.out.println(response.getStatusLine());
            HttpEntity entity = response.getEntity();
            InputStream input = entity.getContent();
            
            byte[] buf = new byte[1024];
            int len = input.read(buf);
            
            System.out.println(new String(buf, 0, len));
            EntityUtils.consume(entity);
        } catch (Exception e) {
            // TODO: handle exception
        } finally{
            httpGet.releaseConnection();
        }
    }

}

 

posted @ 2013-01-10 20:49  hnrainll  阅读(494)  评论(0编辑  收藏  举报