post请求xml参数的方法

一般post发送请求,参数都是键值对的形式,但是如果请求参数是以xml形式的话,是没有键的。请求方法如下:

httppost.setEntity(new StringEntity("<buffalo-call>\n" +
                    "<method>getPhsSmsCode</method>\n" +
                    "<map>\n" +
                    "<type>java.util.HashMap</type>\n" +
                    "<string>PHONENUM</string>\n" +
                    "<string>12345678910</string>\n" +
                    "<string>PRODUCTID</string>\n" +
                    "<string>50</string>\n" +
                    "<string>CITYCODE</string>\n" +
                    "<string>0817</string>\n" +
                    "</map>\n" +
                    "\n" +
                    "</buffalo-call>", "text/xml"));

发送post请求的例子:

public static String postUrl(CloseableHttpClient httpClient, String url, String[][] params, String xmlParam, String[][] headers){
        String ret = null;
        // 创建httppost
        HttpPost httppost = new HttpPost(url);
        // 设置请求头信息
        if (headers != null) {
            for (int i=0;i<headers.length;i++) {
                httppost.addHeader(headers[i][0], headers[i][1]);
            }
        }
        // 创建参数队列
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        for (int i=0;i<params.length;i++){
            formparams.add(new BasicNameValuePair(params[i][0], params[i][1]));
        }
        UrlEncodedFormEntity uefEntity;
        try {
            uefEntity = new UrlEncodedFormEntity(formparams, "UTF-8");
            httppost.setEntity(uefEntity);
            //以xml方式提交参数
            if (xmlParam != null) {
                httppost.setEntity(new StringEntity(xmlParam, "text/xml"));
            }
            System.out.println("executing request " + httppost.getURI());
            CloseableHttpResponse response = httpClient.execute(httppost);
            try {
                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    HttpEntity entity = response.getEntity();
                    ret = EntityUtils.toString(entity, "UTF-8");
                }
            } finally {
                response.close();
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (UnsupportedEncodingException e1) {
            e1.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            // 关闭连接,释放资源
//            try {
//                httpClient.close();
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
        }
        return ret;
    }


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值