用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

http请求SOAP webService接口

2018-09-17 作者: 云代码会员举报

[java]代码库

static int socketTimeout = 300000;// 请求超时时间
    static int connectTimeout = 300000;// 传输超时时间

    /**
     * 使用SOAP1.1发送消息
     *
     * @param postUrl
     * @param mapXml
     * @param soapAction
     * @return
     */
    public static String doPostSoap1_1(String postUrl, Map<String, Object> mapXml,
                                       String soapAction) {
        String retStr = "";
        String xml = "";
        // 创建HttpClientBuilder
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        // HttpClient
        CloseableHttpClient closeableHttpClient = httpClientBuilder.build();
        HttpPost httpPost = new HttpPost(postUrl);
        //  设置请求和传输超时时间
        RequestConfig requestConfig = RequestConfig.custom()
                .setSocketTimeout( socketTimeout )
                .setConnectTimeout( connectTimeout ).build();
        httpPost.setConfig(requestConfig);
        try {
            httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8" );
            httpPost.setHeader("SOAPAction", soapAction );

            xml += "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://service.vshop.zjjzfy.com/\">\n" +
                    "   <soapenv:Header/>\n" +
                    "   <soapenv:Body>\n" +
                    "      <ser:" + soapAction + ">\n";
            int i = 0;
            for ( String key : mapXml.keySet() ) {
                xml += "         <arg" + i + "><![CDATA[" + mapXml.get( key ) + "]]></arg" + i + ">\n";
                i++;
            }
            xml += "      </ser:" + soapAction + ">\n" +
                    "   </soapenv:Body>\n" +
                    "</soapenv:Envelope>";
            System.out.println(xml);

            StringEntity data = new StringEntity( xml, Charset.forName("UTF-8") );
            httpPost.setEntity(data);
            CloseableHttpResponse response = closeableHttpClient
                    .execute(httpPost);
            HttpEntity httpEntity = response.getEntity();
            if (httpEntity != null) {
                // 打印响应内容
                retStr = EntityUtils.toString(httpEntity, "UTF-8");
            }
            // 释放资源
            closeableHttpClient.close();
        } catch (Exception e) {
            System.out.println("exception in doPostSoap1_1" + e);
        }

        retStr = retStr.substring( retStr.indexOf("<return>") + 8, retStr.indexOf("</return>"));
        retStr = StringEscapeUtils.unescapeHtml( retStr );
        return retStr;
    }


    public final static String POST_URL = "http://localhost:8080/services/ruirentang";

    public static void main(String[] args) {

        //多参字符串xml请求
        String pre_id = "1009022100";
        String ter_id = "小票机";
        String card_id = "6217002020036666666";
        Map<String, Object> param = new HashMap<>();
        param.put( "pre_id", pre_id );
        param.put( "ter_id", ter_id );
        param.put( "card_id", card_id );

        String msg = doPostSoap1_1( POST_URL, param, "test" );
        System.out.println(msg);

        //一参xml格式请求
        Info p = new Info();
        p.setPre_id("1009022100");
        p.setTer_id("小票机");
        p.setCard_id("6217002020036666666");
        p.setCust_id("81900000994");
        Infos infos = new Infos();
        infos.setInfo(p);

        //把对象转化为 XML,并且设置别名
        String xml = XStreamUtil.beanToXmlWithTag(infos);
        param = new HashMap<>();
        param.put( "xml", xml );

        //把对象转化为 XML,并且设置别名
        msg = doPostSoap1_1( POST_URL, param, "jf_query" );
        System.out.println(msg);
    }


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...