HttpClient 发送请求和参数

 

发送请求 没有参数

private static void getData() {
    String timeStamp = String.valueOf(System.currentTimeMillis());
    String code = "1234";
    String key = "1234567";
    String sign = DigestUtils.md5Hex(key + timeStamp + code).toUpperCase();
    String url = "http://test/api/Information/getData?ts=" + timeStamp + "&code=" + code + "&sign=" + sign;
    CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
    HttpPost httpPost = new HttpPost(url);
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = null;
    try {
        responseBody = closeableHttpClient.execute(httpPost, responseHandler);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            closeableHttpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    System.out.println(responseBody);
//        JSONObject response = new JSONObject(responseBody);
//        JSONArray resultArray = response.getJSONArray("result");
//        for (int i = 0; i < resultArray.length(); i++) {
//            System.out.println(resultArray.getJSONObject(i));
//        }
//        System.out.println("/n/n" +responseBody);
}

 

发送请求 有参数

private static void syncData(String sampleId, String productId, String orderId, int baResult, int isPresentation) {
    String timeStamp = String.valueOf(System.currentTimeMillis());
    String code = "1234";
    String key = "1234567";
    // 添加 Http post 参数
    List<String> jsonList = new ArrayList<String>();
    JSONObject jsonObject = new JSONObject();
    jsonObject.put("sampleId", sampleId);  // 参数
    jsonObject.put("productId", productId);  // 参数
    jsonObject.put("orderId", orderId);  // 参数
    jsonObject.put("baResult", baResult);  // 参数
    jsonObject.put("isPresentation", isPresentation);  // 参数
    String json = jsonObject.toString();
    jsonList.add(json);  // 把 json 放到集合里
    String sign = DigestUtils.md5Hex(key + timeStamp + code + jsonList.toString()).toUpperCase();
    String url = "http://test/api/Information/syncData?ts=" + timeStamp + "&code=" + code + "&sign=" + sign;
    CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
    HttpPost httpPost = new HttpPost(url);
    try {
        httpPost.setEntity(new StringEntity(jsonList.toString()));
    } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
    }
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = null;
    try {
        responseBody = closeableHttpClient.execute(httpPost, responseHandler);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            closeableHttpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    System.out.println(responseBody);
}

 

发送请求 上传pdf文件

private static void uploadPresentation(String productId, String orderId, String reportFile) {
    File file = new File(reportFile);
    MultipartEntityBuilder multipartEntityBuilder = MultipartEntityBuilder.create();
    multipartEntityBuilder.addBinaryBody("file", file);  // 上传的 pdf 报告
    multipartEntityBuilder.addTextBody("productId", productId);  // 参数
    multipartEntityBuilder.addTextBody("orderId", orderCode);  // 参数
    HttpEntity httpEntity = multipartEntityBuilder.build();
    String timeStamp = String.valueOf(System.currentTimeMillis());
    String code = "1234";
    String key = "1234567";
    String sign = DigestUtils.md5Hex(key + timeStamp + code).toUpperCase();
    String url = "http://test/Information/uploadfile?ts=" + timeStamp + "&code=" + code + "&sign=" + sign;
    CloseableHttpClient closeableHttpClient = HttpClientBuilder.create().build();
    HttpPost httpPost = new HttpPost(url);
    try {
        httpPost.setEntity(httpEntity);
    } catch (Exception e1) {
        e1.printStackTrace();
    }
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    String responseBody = null;
    try {  
        responseBody = closeableHttpClient.execute(httpPost, responseHandler);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            closeableHttpClient.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    System.out.println(responseBody);
}

  

转载于:https://www.cnblogs.com/0820LL/p/10919943.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值