zoukankan      html  css  js  c++  java
  • HttpClient发送Json数据到指定接口

    项目中遇到将Json数据发送到指定接口,于是结合网上利用HttpClient进行发送.

        /**
         * post发送json数据
         * @param url
         * @param param
         * @return
         */
        private String doPost(String url, JSONObject param) {
            HttpPost httpPost = null;
            String result = null;
            try {
                HttpClient client = new DefaultHttpClient();
                httpPost = new HttpPost(url);
                if (param != null) {
                    StringEntity se = new StringEntity(param.toString(), "utf-8");
                    httpPost.setEntity(se); // post方法中,加入json数据
                    httpPost.setHeader("Content-Type", "application/json");
                }
                
                HttpResponse response = client.execute(httpPost);
                if (response != null) {
                    HttpEntity resEntity = response.getEntity();
                    if (resEntity != null) {
                        result = EntityUtils.toString(resEntity, "utf-8");
                    }
                }
                
            } catch (Exception ex) {
                logger.error("发送到接口出错", ex);
            }
            return result;
        }
  • 相关阅读:
    css3转换
    JavaScript函数
    JavaScript数组
    JavaScript流程控制
    JavaScript数据类型、运算
    css3响应布局
    css3渐变、背景、过渡、分页
    css3初识
    CSS3选择器
    H5拖拽、绘画、web存储
  • 原文地址:https://www.cnblogs.com/fxust/p/8214998.html
Copyright © 2011-2022 走看看