zoukankan      html  css  js  c++  java
  • java post请求form表单格式发送数据,ContentType=multipart/form-data

     /**
         * post请求form表单格式发送数据
         *  multipart/form-data
         * @param url 接口地址
         * @param param 参数数组
         * @return 返回结果
         * @throws IOException
         */
        public static String sendPost(String url, Map<String, String> param) throws IOException {
            CloseableHttpClient httpclient = HttpClients.createDefault();
            String result = "";
            try {
                HttpPost httppost = new HttpPost(url);
                //构建超时等配置信息
                RequestConfig config = RequestConfig.custom().setConnectTimeout(1000) //连接超时时间
                        .setConnectionRequestTimeout(1000) //从连接池中取的连接的最长时间
                        .setSocketTimeout(10 * 1000) //数据传输的超时时间
                        .build();
                httppost.setConfig(config);
                MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
                ContentType strContent = ContentType.create("text/plain", Charset.forName("UTF-8"));
                //参数填充
                for (String key : param.keySet()) {
                    entityBuilder.addTextBody(key, param.get(key),strContent);
                }
                HttpEntity entity = entityBuilder.build();
                httppost.setEntity(entity);
                CloseableHttpResponse response = httpclient.execute(httppost);
                try {
                    HttpEntity resEntity = response.getEntity();
                    //回复接收
                    result = EntityUtils.toString(resEntity, "UTF-8");
                } finally {
                    response.close();
                }
            } catch (Exception e) {
                logger.error(e.getMessage());
                throw e;
            } finally {
                httpclient.close();
            }
            return result;
        }
    

      

  • 相关阅读:
    服务器文档下载zip格式
    关于精度,模运算和高精的问题//19/07/14
    Luogu P2010 回文日期 // 暴力
    树形DP水题集合 6/18
    普通背包水题集合 2019/6/17
    因为时间少
    重学树状数组6/14(P3368 【模板】树状数组 2)
    Luogu P1291 [SHOI2002]百事世界杯之旅 // 易错的期望
    Luogu P4316 绿豆蛙的归宿//期望
    树剖
  • 原文地址:https://www.cnblogs.com/webttt/p/14605086.html
Copyright © 2011-2022 走看看