zoukankan      html  css  js  c++  java
  • 后台http请求参数为json 【转】

    代码实现:

    /**
         * 向指定 URL 发送POST方法的请求(数据格式为json)
         *
         * @param url 发送请求的 URL
         * @param paramMap 请求参数集合
         * @param paramMap 请求参数,请求参数为json的形式。
         * @return 所代表远程资源的响应结果
         */
        public static String sendPost(String url, Map<String, Object> paramMap) {
            CloseableHttpClient httpClient = null;
            CloseableHttpResponse httpResponse = null;
            String result = "";
            // 创建httpClient实例
            httpClient = HttpClients.createDefault();
            // 创建httpPost远程连接实例
            HttpPost httpPost = new HttpPost(url);
    
            // 配置请求参数实例
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(35000)// 设置连接主机服务超时时间
                    .setConnectionRequestTimeout(35000)// 设置连接请求超时时间
                    .setSocketTimeout(60000)// 设置读取数据连接超时时间
                    .build();
            // 为httpPost实例设置配置
            httpPost.setConfig(requestConfig);
            // 设置请求头
            httpPost.addHeader("Content-Type", "application/json");
            httpPost.addHeader("appid", "avkp4qyu01fki");
    
    
            // 封装post请求参数
            JSONObject jsonObject = new JSONObject();
            if (null != paramMap && paramMap.size() > 0) {
                //List<NameValuePair> nvps = new ArrayList<NameValuePair>();
                // 通过map集成entrySet方法获取entity
                Set<Map.Entry<String, Object>> entrySet = paramMap.entrySet();
                // 循环遍历,获取迭代器
                Iterator<Map.Entry<String, Object>> iterator = entrySet.iterator();
                while (iterator.hasNext()) {
                    Map.Entry<String, Object> mapEntry = iterator.next();
                    //nvps.add(new BasicNameValuePair(mapEntry.getKey(), mapEntry.getValue().toString()));
                    jsonObject.put(mapEntry.getKey(),mapEntry.getValue());
                }
                ContentType contentType = ContentType.create("application/json");
                // 为httpPost设置封装好的请求参数
                try {
                    //设置参数的content-type格式
                    httpPost.setEntity(new StringEntity(jsonObject.toString(), contentType));
                    //此方式参数content-type为application/x-www-form-urlencoded,源码中默认实现的
                    //httpPost.setEntity(new UrlEncodedFormEntity(nvps,"utf-8"));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            try {
                // httpClient对象执行post请求,并返回响应参数对象
                httpResponse = httpClient.execute(httpPost);
                // 从响应对象中获取响应内容
                HttpEntity entity = httpResponse.getEntity();
                result = EntityUtils.toString(entity);
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                // 关闭资源
                if (null != httpResponse) {
                    try {
                        httpResponse.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                if (null != httpClient) {
                    try {
                        httpClient.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
            return result;
        }
    ————————————————
    版权声明:本文为CSDN博主「gxk2019」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
    原文链接:https://blog.csdn.net/gxkvji/article/details/97966198

    通过HttpservletRequest接收json数据

            // 接收json数据
            BufferedReader streamReader = new BufferedReader(new InputStreamReader(
                    req.getInputStream(), "UTF-8"));
            StringBuilder responseStrBuilder = new StringBuilder();
            String inputStr;
            while ((inputStr = streamReader.readLine()) != null)
                responseStrBuilder.append(inputStr);
            // 转化成json对象
            JSONObject jsonObject = JSONObject.fromObject(responseStrBuilder
                    .toString());            
  • 相关阅读:
    微信小程序 原生框架 (分享方法封装)
    JavaScript 正则表达式学习笔记
    JavaScript Date
    JavaScript String
    JavaScript Array
    前端开发缓存问题的解决方案
    参数验证
    html中的关于距离的总结大全
    Vuex的辅助函数mapState, mapActions, mapMutations用法
    Vue的内置组件transition
  • 原文地址:https://www.cnblogs.com/licheng0201/p/12103225.html
Copyright © 2011-2022 走看看