美文网首页
Java Http Post工具

Java Http Post工具

作者: 木木111314 | 来源:发表于2022-03-28 16:28 被阅读0次

Java Http Post工具
允许设置header、参数、上传文件


    public static String doPost(String url, Map<String, String> map, String charset, Map<String, String> headers, Map<String, File> files) {
        HttpEntity httpEntity = null;
        if (files != null && files.size() > 0) {
            logger.debug("上传文件");
            MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();

 
            Iterator fileInterator = files.entrySet().iterator();
            while (fileInterator.hasNext()) {
                Map.Entry<String, File> elem = (Map.Entry<String, File>) fileInterator.next();
                entityBuilder.addBinaryBody(elem.getKey(), elem.getValue());
            }

 
            if (map != null) {
                Iterator iterator = map.entrySet().iterator();
                while (iterator.hasNext()) {
                    Map.Entry<String, String> elem = (Map.Entry<String, String>) iterator.next();
                    entityBuilder.addTextBody(elem.getKey(), elem.getValue());
                }
            }

            httpEntity = entityBuilder.build();
        }
   
        HttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        try {
            httpClient = new SSLClient();
            url = CodeUtils.encodeURIComponentClear(url);
            logger.debug(url);
            httpPost = new HttpPost(url);
            // 设置参数
            if (httpEntity == null) {
                List<NameValuePair> list = new ArrayList<NameValuePair>();
                if (map != null) {
                    Iterator iterator = map.entrySet().iterator();
                    while (iterator.hasNext()) {
                        Map.Entry<String, String> elem = (Map.Entry<String, String>) iterator.next();
                        list.add(new BasicNameValuePair(elem.getKey(), elem.getValue()));
                    }
                    if (list.size() > 0) {
                        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list, charset);
                        httpPost.setEntity(entity);
                    }
                }

            } else {
                httpPost.setEntity(httpEntity);
            }

            if (headers != null) {
                Set<String> keys = headers.keySet();
                for (String key : keys) {
                    httpPost.setHeader(key, headers.get(key));
                }
            }
            HttpResponse response = httpClient.execute(httpPost);
            if (response != null) {
                HttpEntity resEntity = response.getEntity();
                if (resEntity != null) {
                    result = EntityUtils.toString(resEntity, charset);
                }
            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return result;
    }

相关文章

网友评论

      本文标题:Java Http Post工具

      本文链接:https://www.haomeiwen.com/subject/bzvpjrtx.html