HttpClient工具使用

HttpClient工具简单封装
public class HttpClientUtils {

    /**
     * 带token的post请求
     * @param url
     * @param jsonParam
     * @param token
     * @return
     */
    public static String doPostJson(String url, String jsonParam, String token) {
        CloseableHttpClient client = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            HttpPost post = new HttpPost(url);
            StringEntity entity = new StringEntity(jsonParam, ContentType.APPLICATION_JSON);
            entity.setContentEncoding("UTF-8");
            post.setEntity(entity);
            post.addHeader("token",token);
            response = client.execute(post);
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        return resultString;
    }
    
    /**
     * 上传文件
     * @param url
     * @param taskName
     * @param token
     * @return
     */
    public static String doFilePost(String url, String taskName, String token) {
        CloseableHttpClient client = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Post请求
            HttpPost post = new HttpPost(url);
            post.addHeader("token",token);
            //FileBody bin = new FileBody(new File("C:\\Users\\Desktop\\single.xls"));
            // 解决中文乱码问题
            ContentType contentType = ContentType.create("text/plain", Charset.forName("UTF-8"));
            HttpEntity reqEntity = MultipartEntityBuilder.create().setMode(HttpMultipartMode.BROWSER_COMPATIBLE)
                    .addBinaryBody("multipartfiles", new File("D:\\workspace\\createdata\\src\\main\\resources\\single_P.xls"))
                    .addTextBody("taskName", taskName, contentType)
                    .addTextBody("issueAttribute", "2").build();

            post.setEntity(reqEntity);
            // 执行请求
            response = client.execute(post);
            // 判断返回状态是否为200
            if (response.getStatusLine().getStatusCode() == 200) {
                resultString = EntityUtils.toString(response.getEntity(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (response != null) {
                    response.close();
                }
                client.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return resultString;
    }
    /**
     * 获取token信息
     * @param url
     * @param jsonParam
     * @return
     */
     
    public static String getToken(String url, String jsonParam){
        String token = null;
        CloseableHttpClient client = HttpClients.createDefault();
        HttpPost post = new HttpPost(url);
        StringEntity entity = new StringEntity(jsonParam, ContentType.APPLICATION_JSON);
        post.setEntity(entity);
        try {
            CloseableHttpResponse response = client.execute(post);
            Header[] headers = response.getHeaders("token");
            token = headers[0].getValue();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return token;
    }
    public static void main(String[] args) {
    }

}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值