工作中有时会对接WebService的接口,考虑过CXF,Axis等框架,但只是调用的话用HTTP就够了

简单介绍一下相关概念

WebService:一种轻量级的独立的通讯技术,跨语言,跨平台,通过SOAP在Web上提供服务,使用WSDL文件进行说明

SOAP:WebService的通信协议,简单对象存取协议,SOAP是XML形式的调用规范,它是基于HTTP协议的

XML:可扩展型标记语言。面向短期的临时数据处理、面向万维网络,是Soap的基础,它并不适合大数据量的交互

WSDL:一个 XML 文档,简单来说就是说明这个WebService接口的调用方法,用ASP.NET平台开发的接口会有.asmx为后缀的文本文件,和WSDL并无区别

进入正题

1、由于用的是Maven管理jar包,先引入相关依赖

<!-- apache http-->
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpcore</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpmime</artifactId>
        </dependency>

2、封装HTTP调用的工具类

两个参数,1、url:请求的WSDL地址(或ASMX)  2、content:请求的完整XML报文

返回值是一个XML的字符串

/**
     * 使用apache的HttpClient发送http请求
     * @param url     请求URL
     * @param content   请求参数
     * @Author: WgRui
     * @Date: 2020/12/16
     * @return XML String
     */
    public static String httpCilentPost(String url, String content) throws IOException {

        // 获得Http客户端
        CloseableHttpClient httpClient = HttpClientBuilder.create().build();

        // 创建Post请求
        HttpPost httpPost = new HttpPost(url);

        // 将数据放入entity中
        StringEntity entity = new StringEntity(content, "UTF-8");
        httpPost.setEntity(entity);

        // 响应模型
        String result = null;
        CloseableHttpResponse response = null;

        try {

            //设置请求头
            //特别说明一下,此处为SOAP1.1协议
            //如果用的是SOAP1.2协议,改为:"application/soap+xml;charset=UTF-8"
            httpPost.setHeader("Content-Type", "text/xml;charset=UTF-8");

            //命名空间+方法名
            //如为SOAP1.2协议不需要此项
            httpPost.setHeader("SOAPAction", "\"http://tempuri.org/Request\"");

            // 由客户端执行(发送)Post请求
            response = httpClient.execute(httpPost);

            // 从响应模型中获取响应实体
            HttpEntity responseEntity = response.getEntity();

            log.info("响应ContentType为:" + responseEntity.getContentType());
            log.info("响应状态为:" + response.getStatusLine());

            if (responseEntity != null) {
                result = EntityUtils.toString(responseEntity);
                log.info("响应内容为:" + result);
            }

        } finally {

            // 释放资源
            if (httpClient != null) {
                httpClient.close();
            }
            if (response != null) {
                response.close();
            }

        }

        return result;

    }

3、确定你的调用地址和请求报文,这些是要和提供方的工程师沟通清楚的

我以一个互联网上开放的获取随机字符的一个WebService接口为例

这里推荐一个工具,SOAP UI,可以很方便地生成请求报文和测试WebService接口

测试类写一个main方法调用

import java.io.IOException;

public class Test {

    public static void main(String[] args) throws IOException {

        //请求地址
        String url = "http://www.webxml.com.cn/WebServices/RandomFontsWebService.asmx";

        //请求报文
        String content = "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:web=\"http://WebXml.com.cn/\">\n"+
                            "<soapenv:Header/>\n"+
                            "<soapenv:Body>\n"+
                                "<web:getChineseFonts>\n"+
                                    "<web:byFontsLength>5</web:byFontsLength>\n"+
                                "</web:getChineseFonts>\n"+
                            "</soapenv:Body>\n"+
                        "</soapenv:Envelope>";

        //调用
        String result = HttpSendUtil.httpCilentPost(url, content);

        
    }

}

调用成功,控制台打印出结果

springBoot 调用SOAP接口 发送xml格式字符串参数 java调用soap接口_http

然后就大功告成了