• 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);
    }

      

  • 相关阅读:
    全局数据库名称/数据库实例/SID 的区别
    【转载】ORACLE 10G DBCA创建脚本实现手动创建数据库
    apue 20130328
    apue 20130323
    visual c++6.0
    C语言
    apue 20130322
    apue 20130324
    apue 20130325
    C语言里的字符串解析
  • 原文地址:https://www.cnblogs.com/0820LL/p/10919943.html
Copyright © 2020-2023  润新知