HTTPClientUtil工具类

public  static  String sendPut(String url ,Map<String ,Object> map ){
        //谷歌的Gson
        Gson gson = new Gson();
        //相对于commons-httpclient 3.1这里采用接口的方式来获取httpclient了
        HttpClient httpClient = HttpClients.createDefault();
        //声明请求方式
        HttpPut httpPut = new HttpPut(url);
        //设置消息头
        httpPut.setHeader("Content-Type","application/json;charset=utf-8");
        httpPut.setHeader("Accept","application/json");
        //设置发送数据(数据尽量为json),可以设置数据的发送时的字符集
        httpPut.setEntity(new StringEntity(gson.toJson(map),"utf-8"));
        //获取相应数据,这里可以获取相应的数据
        HttpResponse httpResponse = null;
        String result = "";
        try {
            httpResponse = httpClient.execute(httpPut);
            //拿到实体
            HttpEntity httpEntity= httpResponse.getEntity();
            //获取结果,这里可以正对相应的数据精细字符集的转码

            if (httpEntity != null) {
                result = EntityUtils.toString(httpEntity,"utf-8");
            }
            //关闭连接
            httpPut.releaseConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result ;
    }
   public  static  String  sendPost(String url ,Map<String ,Object> map) {
        //谷歌的Gson
        Gson gson = new Gson();
        //相对于commons-httpclient 3.1这里采用接口的方式来获取httpclient了
        HttpClient httpClient = HttpClients.createDefault();
        //声明请求方式
        HttpPost httpPost = new HttpPost(url);
        //设置消息头
        httpPost.setHeader("Content-Type","application/json;charset=utf-8");
        httpPost.setHeader("Accept","application/json");
        //设置发送数据(数据尽量为json),可以设置数据的发送时的字符集
        httpPost.setEntity(new StringEntity(gson.toJson(map),"utf-8"));
        //获取相应数据,这里可以获取相应的数据
        HttpResponse httpResponse = null;
        String result = "";
        try {
            httpResponse = httpClient.execute(httpPost);
            //拿到实体
            HttpEntity httpEntity= httpResponse.getEntity();
            //获取结果,这里可以正对相应的数据精细字符集的转码

            if (httpEntity != null) {
                result = EntityUtils.toString(httpEntity,"utf-8");
            }
            //关闭连接
            httpPost.releaseConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  result ;
    }
    public  static  String  post(String url ,Map<String ,Object> map) {
        //谷歌的Gson
        Gson gson = new Gson();
        //相对于commons-httpclient 3.1这里采用接口的方式来获取httpclient了
        HttpClient httpClient = HttpClients.createDefault();
        //声明请求方式
        HttpPost httpPost = new HttpPost(url);
        //设置消息头
        httpPost.setHeader("Content-Type","application/json;charset=UTF-8");
        httpPost.setHeader("X-Requested-With","XMLHttpRequest");
        httpPost.setHeader("Authorization","YXBp");
        //设置发送数据(数据尽量为json),可以设置数据的发送时的字符集
        httpPost.setEntity(new StringEntity(gson.toJson(map),"utf-8"));
        //获取相应数据,这里可以获取相应的数据
        HttpResponse httpResponse = null;
        String result = "";
        try {
            httpResponse = httpClient.execute(httpPost);
            //拿到实体
            HttpEntity httpEntity= httpResponse.getEntity();
            //获取结果,这里可以正对相应的数据精细字符集的转码

            if (httpEntity != null) {
                result = EntityUtils.toString(httpEntity,"utf-8");
            }
            //关闭连接
            httpPost.releaseConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  result ;
    }
    public  static  String sendGet(String url,Map<String,String> headMap){
        //相对于commons-httpclient 3.1这里采用接口的方式来获取httpclient了
        HttpClient httpClient = HttpClients.createDefault();
        //声明请求方式
        HttpGet httpGet = new HttpGet(url);
        if(headMap!=null){
            //设置消息头
            for (String name : headMap.keySet()) {
                httpGet.setHeader(name,headMap.get(name));
            }
        }
        //获取相应数据,这里可以获取相应的数据
        HttpResponse httpResponse = null;
        String result = "";
        try {
            httpResponse = httpClient.execute(httpGet);
            //拿到实体
            HttpEntity httpEntity= httpResponse.getEntity();
            //获取结果,这里可以正对相应的数据精细字符集的转码

            if (httpEntity != null) {
                result = EntityUtils.toString(httpEntity,"utf-8");
            }
            //关闭连接
            httpGet.releaseConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  result ;
    }
    public  static String sendPost(String url ,Map<String ,Object> map,Map<String,String> headMap){
        //谷歌的Gson
        Gson gson = new Gson();
        //相对于commons-httpclient 3.1这里采用接口的方式来获取httpclient了
        HttpClient httpClient = HttpClients.createDefault();
        //声明请求方式
        HttpPost httpPost = new HttpPost(url);
        //设置消息头
        for (String name : headMap.keySet()) {
            httpPost.setHeader(name,headMap.get(name));
        }
        //设置发送数据(数据尽量为json),可以设置数据的发送时的字符集
        httpPost.setEntity(new StringEntity(gson.toJson(map),"utf-8"));
        //获取相应数据,这里可以获取相应的数据
        HttpResponse httpResponse = null;
        String result = "";
        try {
            httpResponse = httpClient.execute(httpPost);
            //拿到实体
            HttpEntity httpEntity= httpResponse.getEntity();
            //获取结果,这里可以正对相应的数据精细字符集的转码

            if (httpEntity != null) {
                result = EntityUtils.toString(httpEntity,"utf-8");
            }
            //关闭连接
            httpPost.releaseConnection();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  result ;
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值