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

      

  • 相关阅读:
    2016年3月9日~10日,杨学明老师为武汉某著名通信企业提供内训课程服务!
    打造高效率产品测试体系--产品测试管理(深圳,2016.3.18~19)
    互联网产品上线前,做些什么——产品、开发、测试的视角
    2016年1月16日,《互联网项目管理高级实务》内训在上海某高科技企业成功举办!
    用C++对C++语法格式进行分析
    mysql主从配置
    Windows Zip/CentOS/Radhat系统安装Mysql5.7.x方法
    c++预声明类引发的无法解析外部符号问题
    解决VS2015单元测试“未能设置用于运行测试的执行上下文”问题
    扩展Linux磁盘空间
  • 原文地址:https://www.cnblogs.com/0820LL/p/10919943.html
Copyright © 2011-2022 走看看