zoukankan      html  css  js  c++  java
  • 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);
    }

      

  • 相关阅读:
    python线程与进程手记
    3.08课·········switch case及if else嵌套(日期格式)
    3.07课·········if分支语句
    3.06课·········C#语言基础
    3.05课·········进制转换
    Oracle profile 使用技巧
    sys用户密码丢失找回密码的步骤和命令
    oracle帐号scott被锁定如何解锁
    SQL中哪些情况会引起全表扫描
    Oracle创建用户、角色、授权、建表
  • 原文地址:https://www.cnblogs.com/0820LL/p/10919943.html
Copyright © 2011-2022 走看看