四、简单的HttpGet请求、HttpPost请求、HttpHead请求案例

httpclient 支持http1.1的六种请求方法,options,put,delete,trace,get,head,post,这里只列举常用的get,head和post请求

 

1. Get请求 

 

/**
 * 简单的get请求
 * GET 请求指定的页面信息,并返回实体主体
 * @throws IOException
 */
public void getDemo() throws IOException {
    //实例化httpclient,(4.5新版本和以前不同),实例化方式有两种
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpGet httpGet = new HttpGet("http://www.baidu.com");
    httpGet.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:60.0) Gecko/20100101 Firefox/60.0");
    CloseableHttpResponse response = null;
    try {
        /**
         * 底层http链接仍由响应对象保存
         * 允许直接从网络套接字流式传输响应内容
         * 为了确保正确释放系统资源
         * 用户必须从finally子句中调用CloseableHttpResponse #close()
         */
        response = httpClient.execute(httpGet);
        System.out.println(response.getStatusLine());
        HttpEntity entity = response.getEntity();
        //对响应主体做一些有用的事情
        //并确保他完全被消耗掉
        EntityUtils.toString(entity,"utf-8");
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        response.close();
    }
}

 

2.Post请求

 

/**
 * 简单的post请求
 * 1.谷歌浏览器抓包登陆页,(勾选preserve log)能防止登陆页跳转刷新
 * 2.查看登陆需要传递的参数
 * 3.将参数以key,value的方式写进List<NameValuePair> nvp = new ArrayList<NameValuePair>();
 *
 * POST 向指定资源提交数据进行处理请求(例如提交表单或者上传文件)。数据被包含在请求体中。
 * POST请求可能会导致新的资源的建立和/或已有资源的修改。
 */
public void postDemo() {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpPost httpPost = new HttpPost("http://wiki.acegear.com/dologin.action");
    httpPost.setHeader("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.13; rv:60.0) Gecko/20100101 Firefox/60.0");
    List<NameValuePair> nvp = new ArrayList<NameValuePair>();
    nvp.add(new BasicNameValuePair("os_username", "username"));
    nvp.add(new BasicNameValuePair("os_password", "password"));
    CloseableHttpResponse response = null;
    String postEntity = null;
    try {
        httpPost.setEntity(new UrlEncodedFormEntity(nvp));
        response = httpClient.execute(httpPost);
        System.out.println(response.getStatusLine());
        HttpEntity entity = response.getEntity();
        postEntity = EntityUtils.toString(entity,"utf-8");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            response.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    System.out.println(postEntity);
}

 

3.head请求 

 

/**
 * 简单的head请求
 * HEAD 类似于get请求,只不过返回的响应中没有具体的内容,用于获取报头
 * @throws IOException
 */
public void headDemo() throws IOException {
    CloseableHttpClient httpClient = HttpClients.createDefault();
    HttpHead httpHead = new HttpHead("http://www.baidu.com");
    CloseableHttpResponse httpResponse = null;
    String entity = null;
    try {
        httpResponse = httpClient.execute(httpHead);
        StatusLine code = httpResponse.getStatusLine();
        Header[] header = httpResponse.getAllHeaders();
        for(Header header1 : header){
            System.out.println(header1);
        }
        //System.out.println(code);
        //响应内容为空
        //entity = EntityUtils.toString(httpResponse.getEntity(),"utf-8");
    } catch (IOException e) {
        e.printStackTrace();
    }finally {
        httpResponse.close();
    }
    System.out.println(entity);
}

HEAD和GET本质是一样的,区别在于HEAD不含有呈现数据,而仅仅是HTTP头信息。有的人可能觉得这个方法没什么用,其实不是这样的。想象一个业务情景:欲判断某个资源是否存在,我们通常使用GET,但这里用HEAD则意义更加明确。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值