weixin_40280237 2018-01-25 08:19 采纳率: 0%
浏览 3182
已结题

急!在线等!使用java实现发送json报文的post请求方法???

急!在线等!使用java实现发送json报文的post请求方法???各位大神帮帮忙啊~~~
报文如下:
{
"busiParams": {
"serviceNum": "24027000071563"
},
"pubInfo": {
"interfaceId": "1",
"transactionId": "WEB20170707105545257737",
"interfaceType": "52",
"opId": "5010",
"countyCode": "110",
"orgId": "900000055",
"clientIP": "1",
"transactionTime": "20160512175523",
"regionCode": "110"
}
}
我的代码实现如下,这个代码是有问题的,麻烦给看看啊:

//----------------------post请求----------------------------------
public Map post(String host, int port, String path){
Map result = new HashMap();
URIBuilder builder = new URIBuilder();

        String jsonStr = "{\"busiParams\":{\"serviceNum\":\"24027000071563\"},\"pubInfo\":{\"interfaceId\":\"1\",\"transactionId\":\"WEB20170707105545257737\",\"interfaceType\":\"52\",\"opId\":\"5010\",\"countyCode\":\"110\",\"orgId\":\"900000055\",\"clientIP\":\"1\",\"transactionTime\":\"20160512175523\",\"regionCode\":\"110\"}}";   
        JSONObject jsonObject =JSONObject.parseObject(jsonStr);


        StringEntity entity = null;
        try {
            entity = new StringEntity(jsonStr);
        } catch (Exception e1) {
            e1.printStackTrace();
        }


        builder.setScheme("http");
        builder.setHost(host);
        if (port != 0){
            builder.setPort(port);
        }
        if (path != null){
            builder.setPath(path);
        }
        if (paras.size()>0){
            List<NameValuePair> pars = new ArrayList<NameValuePair>();
            for (String key : paras.keySet()) {
                pars.add(new BasicNameValuePair(key, paras.get(key)));
            }
            try {

                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/json");//发送json数据需要设置contentType



            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        HttpPost post;
        try {
            post = new HttpPost(builder.build());
            if(headers.size()>0){
                for (String key : headers.keySet()) {
                    post.setHeader(key, headers.get(key));
                }
            }
            if (entity!=null){

                post.setEntity(entity);
            }
            HttpResponse response = client.execute(post);
            result.put("code", String.valueOf(response.getStatusLine().getStatusCode()));
            InputStream in = response.getEntity().getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(in));
            StringBuffer buffer = new StringBuffer();
            String line = null;
            while ((line = reader.readLine())!=null){
                buffer.append(line);
            }

            result.put("text", buffer.toString());

        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return result;
    }
  • 写回答

7条回答 默认 最新

  • 仲宇 2018-01-26 04:37
    关注

    我的post一段JSON是这样的:
    /**
    * post请求
    * @param url url地址
    * @param jsonParam 参数
    * @return
    */
    public static int httpPost(String url,String jsonParam){
    //post请求
    DefaultHttpClient httpClient = new DefaultHttpClient();

        HttpPost method = new HttpPost(url);
    
        try {
            if (null != jsonParam) {
                //解决中文乱码问题
                StringEntity entity = new StringEntity(jsonParam, "utf-8");
                entity.setContentEncoding("UTF-8");
                entity.setContentType("application/json");
                method.setEntity(entity);
            }
            HttpResponse result = httpClient.execute(method);
            url = URLDecoder.decode(url, "UTF-8");
            /**请求发送成功,并得到响应**/
            if (result.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                String str = "";
                try {
                    /**读取服务器返回过来的json字符串数据**/
                    str = EntityUtils.toString(result.getEntity());
                    Log.info("[" + url + ":" + str + "]");
                } catch (Exception e) {
                    //logger.error("post请求提交失败:" + url, e);
                    return 0;
                }
            }
            else
            {
                return 0;
            }
        } catch (IOException e) {
            Log.info(e.getMessage());
            return 0;
        }
    
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab有关常微分方程的问题求解决
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable