HttpClient post 与get

HttpClient

了解get和post区别,参考这里

无非是代码模拟人工点击浏览器

3个步骤

  1. 打开浏览器

      CloseableHttpClient httpClient = HttpClients.createDefault();
    
  2. 输入网址

     HttpGet httpGet = new HttpGet("网址....");
    
  3. 回车,返回界面

     CloseableHttpResponse  response = httpClient.execute(httpGet);
    

Get无参

查看百度首页内容

public class HttpTest{

    public static void main(String[] args) {
        //模拟打开浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //创建GET请求,设置URL访问地址
        HttpGet httpGet = new HttpGet("http://www.baidu.com");
        CloseableHttpResponse response=null;
        try {
            //使用HttpClient发起请求,获取response
            response = httpClient.execute(httpGet);
            //解析响应
            if(response!=null){
                //输出getStatusLine()
                // 成功 HTTP/1.1 200 OK
                // 失败 HTTP/1.1 403 Forbidden
                //获取状态码,如 200 或者403
                System.out.println(response.getStatusLine().getStatusCode());
                HttpEntity entity = response.getEntity();//实体
                String content = EntityUtils.toString(entity, "utf8");//把实体转为string
                System.out.println(content);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭,节省资源
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

Get有参

输入网址也有参数啊,不能光

http://www.baidu.com 啊

比如访问百度百科,这是有参数的

  https://baike.baidu.com/item/0/11071655?fr=aladdin

由于httpGet的参数也能是URI,而URI的创建需要URIbuilder,所以

public class HttpTest{
    public static void main(String[] args) throws URISyntaxException {
        //模拟打开浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();
        //构造带参数的地址
         //https://baike.baidu.com/item/0/11071655?fr=aladdin
        URIBuilder uriBuilder = new URIBuilder("https://baike.baidu.com/item/0/11071655");
        uriBuilder.setParameter("fr","aladdin");//参数

        //创建GET请求,设置URI访问地址
        HttpGet httpGet = new HttpGet(uriBuilder.build());
        CloseableHttpResponse response=null;
        try {
            //使用HttpClient发起请求,获取response
            response = httpClient.execute(httpGet);
            //解析响应
            if(response!=null){
                System.out.println(response.getStatusLine());
                HttpEntity entity = response.getEntity();
                String content = EntityUtils.toString(entity, "utf8");
                System.out.println(content);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭response
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

无参post

和无参get差不多,只需改变一行

HttpGet httpGet = new HttpGet("网址....");
改为
HttpPost httpPost = new HttpPost("网址....");

带参post

public class HttpTest{

    public static void main(String[] args) throws URISyntaxException, UnsupportedEncodingException {
        //模拟打开浏览器
        CloseableHttpClient httpClient = HttpClients.createDefault();

        //https://baike.baidu.com/item/0/11071655?fr=aladdin
        HttpPost httpPost = new HttpPost("https://baike.baidu.com/item/0/11071655");

        //声明list集合,封装表单的中的参数
        ArrayList<NameValuePair> params = new ArrayList<>();
        params.add(new BasicNameValuePair("fr","aladdin"));

        //创建表单的Entity对象,参数为表单数据和编码
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(params, "utf8");

        //将表单Entity对象加入到post请求中
        httpPost.setEntity(formEntity);

        CloseableHttpResponse response=null;
        try {
            //使用HttpClient发起请求,获取response
            response = httpClient.execute(httpPost);

            //解析响应
            if(response!=null){
                System.out.println(response.getStatusLine());
                HttpEntity entity = response.getEntity();
                String content = EntityUtils.toString(entity, "utf8");
                System.out.println(content);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            //关闭response
            try {
                response.close();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }

    }
}

));

       
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值