首页 > Web开发 > 详细

httpClient POST 请求

时间:2019-03-08 15:14:56      阅读:223      评论:0      收藏:0      [点我收藏+]
public static String doPostJson(String url,Map<String,String> map) {
        // 创建Httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        CloseableHttpResponse response = null;
        String resultString = "";
        try {
            // 创建Http Post请求
            HttpPost httpPost = new HttpPost(url);
            
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();  
            if(map!=null){  
                for (Entry<String, String> entry : map.entrySet()) {  
                    nvps.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));  
                }  
            }  
            //设置参数到请求对象中  
            httpPost.setEntity(new UrlEncodedFormEntity(nvps,"utf-8"));  
            
            
            // 创建请求内容
       //     StringEntity entity = new StringEntity(json, ContentType.APPLICATION_JSON);
       //     httpPost.setEntity(entity);
            // 执行http请求
            response = httpClient.execute(httpPost);
            
            HttpEntity httpEntity = response.getEntity(); 


            
            resultString = EntityUtils.toString(response.getEntity(), "utf-8");
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                response.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        return resultString;
    }

  

httpClient POST 请求

原文:https://www.cnblogs.com/lxnv587/p/10495866.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!