weixin_40280237
weixin_40280237
2018-01-25 08:19

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

5
  • java
  • json

急!在线等!使用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条回答