调用webService几种方式示例

一、使用hutool的SoapClient,代码如下

1)相关引用

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.11</version>
            <scope>compile</scope>
        </dependency>

2)实现代码

package com.lqt.test;

import cn.hutool.core.lang.Console;
import cn.hutool.http.webservice.SoapClient;

import java.io.IOException;
import java.util.HashMap;

public class WebServiceTest {
    public static void main(String[] args) throws IOException {
        String url = "http://ip:端口号/soap/IPacsServices";
        HashMap<String, Object> map = new HashMap<>();
        map.put("OrderNo", "111111");
        map.put("PatName", "test");
        SoapClient client = SoapClient.create(url)
                // 设置要请求的方法,此接口方法前缀为web,传入对应的命名空间
                .setMethod("queryQueue", "urn:PacsServicesIntf-IPacsServices")
                // 设置参数,此处自动添加方法的前缀
                .setParams(map);

        // 发送请求,参数true表示返回一个格式化后的XML内容
        // 返回内容为XML字符串,可以配合XmlUtil解析这个响应
        Console.log(client.send(true));
    }
}
package com.lqt.test;

import cn.hutool.http.webservice.SoapClient;

import java.io.IOException;
import java.util.HashMap;
import java.util.UUID;

public class WebServiceTest {
    public static void main(String[] args) throws IOException {
        String url = "http://ip:端口号/wsdl/IPacsServices";
        String hisResult = "";
        HashMap<String, Object> map = new HashMap<>();
        map.put("OrderNo", "111111");
        map.put("PatName", "test");
        String soapAction = "http:PacsServicesIntf-IPacsServices/queryQueue";
        SoapClient soapClient = SoapClient.create(url).setMethod("queryQueue", soapAction).
                setParams(map);
        soapClient.setReadTimeout(15000);//超时时间15秒。
        soapClient.setConnectionTimeout(5000);//超时时间5秒。
        String uuid = UUID.randomUUID().toString().replaceAll("-", "");
        hisResult = soapClient.send(true).replaceAll("&lt;", "<").replaceAll("&gt;", ">");
        System.out.printf(hisResult);
    }
}

二、使用hutool的HttpUtil.post方式

 1)相关引用

        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>5.7.11</version>
            <scope>compile</scope>
        </dependency>

2)实现代码

package com.lqt.test;

import cn.hutool.http.HttpUtil;

import java.io.IOException;

public class WebServiceTest {
    public static void main(String[] args) throws IOException {
        String url = "http://IP:端口/wsdl/IPacsServices";
        String hisResult = "";

        String paramPattern = "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:PacsServicesIntf-IPacsServices\">\n" +
                "   <soapenv:Header/>\n" +
                "   <soapenv:Body>\n" +
                "      <urn:queryQueue soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" +
                "         <OrderNo xsi:type=\"xsd:string\">111111</OrderNo>\n" +
                "         <PatName xsi:type=\"xsd:string\">test</PatName>\n" +
                "      </urn:queryQueue>\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>";
        hisResult = HttpUtil.post(url, paramPattern,12000).replaceAll("&lt;", "<").replaceAll("&gt;", ">");

        System.out.printf(hisResult);
    }
}

三、apache的HttpClient调用方式

 1)相关引用


        <dependency>
            <groupId>commons-httpclient</groupId>
            <artifactId>commons-httpclient</artifactId>
            <version>3.1</version>
            <scope>compile</scope>
        </dependency>

2)实现代码

package com.lqt.test;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.methods.InputStreamRequestEntity;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.methods.RequestEntity;
import org.dom4j.io.SAXReader;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

public class WebServiceTest {
    public static void main(String[] args) throws IOException {
        String url = "http://IP:端口号/soap/IPacsServices";

        String hisResult = "";
        String paramPattern = "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:PacsServicesIntf-IPacsServices\">\n" +
                "   <soapenv:Header/>\n" +
                "   <soapenv:Body>\n" +
                "      <urn:queryQueue soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" +
                "         <OrderNo xsi:type=\"xsd:string\">11111111</OrderNo>\n" +
                "         <PatName xsi:type=\"xsd:string\">test</PatName>\n" +
                "      </urn:queryQueue>\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>";

        HttpClient httpClient = new HttpClient();
        PostMethod httppost = new PostMethod(url);
        /* 把Soap请求数据添加到PostMethod */
        try {
            httppost.setRequestHeader("Content-Type", "text/xml;charset=UTF-8");
            // 设置SOAPAction
            httppost.setRequestHeader("SOAPAction", "http://tempuri.org/IPacsServices/queryQueue");
            byte[] b = paramPattern.getBytes("utf-8");
            InputStream is = new ByteArrayInputStream(b, 0, b.length);
            RequestEntity re = new InputStreamRequestEntity(is, b.length,"application/soap+xml; charset=utf-8");
            httppost.setRequestEntity(re);
            httpClient.executeMethod(httppost);

            InputStream rs = httppost.getResponseBodyAsStream();
            // 获取返回流并转换为Document
            org.dom4j.Document document = new SAXReader().read(rs, "UTF-8");
            String result = document.asXML();
            hisResult = result.replaceAll("&lt;", "<").replaceAll("&gt;", ">");
        } catch (Exception e) {
//            log.info("getChouxueResponse", e);
        } finally {
            httppost.releaseConnection();
        }
        System.out.println("return:" + hisResult);
    }
}

四、net的http调用方式

 1)相关引用


        <dependency>
            <groupId>org.dom4j</groupId>
            <artifactId>dom4j</artifactId>
            <version>2.1.3</version>
            <scope>compile</scope>
        </dependency>

2)代码实现

package com.lqt.test;

import org.apache.commons.lang.StringUtils;
import org.dom4j.Document;
import org.dom4j.io.SAXReader;

import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class WebServiceTest {
    public static void main(String[] args) {
        String url = "http://IP:端口/soap/IPacsServices?wsdl";
        String paramPattern = "<soapenv:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:PacsServicesIntf-IPacsServices\">\n" +
                "   <soapenv:Header/>\n" +
                "   <soapenv:Body>\n" +
                "      <urn:queryQueue soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\">\n" +
                "         <OrderNo xsi:type=\"xsd:string\">1111111</OrderNo>\n" +
                "         <PatName xsi:type=\"xsd:string\">test</PatName>\n" +
                "      </urn:queryQueue>\n" +
                "   </soapenv:Body>\n" +
                "</soapenv:Envelope>";

        try {
            String host = "IP:端口";
            String SOAPAction = "urn:PacsServicesIntf-IPacsServices#queryQueue";

            //接受返回报文
            String result = new String();
            URL u = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) u.openConnection();
            conn.setDoInput(true);
            //允许对外输出数据
            conn.setDoOutput(true);
            conn.setUseCaches(false);
            conn.setDefaultUseCaches(false);
            conn.setRequestProperty("Host", host);
            conn.setRequestProperty("Content-Type", "text/xml;charset=UTF-8");
            //soap
            conn.setRequestProperty("SOAPAction", SOAPAction);
            conn.setRequestProperty("Content-Length", String.valueOf(paramPattern.length()));
            conn.setRequestMethod("POST");
            //定义输出流
            OutputStream output = conn.getOutputStream();
            if (StringUtils.isNotBlank(paramPattern)) {
                byte[] b = paramPattern.getBytes("UTF-8");
                //发送soap请求报文
                output.write(b, 0, b.length);
                output.flush();
                output.close();
                //定义输入流,获取soap报文
                InputStream input = conn.getInputStream();
                //设置编码格式
                Document document = new SAXReader().read(input, "UTF-8");
                result = document.asXML();
                input.close();
            }
            System.out.println("请求返回报文:" + result);
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}

PS:访问用的url,使用saopUI中的url

有些厂商提供的wsdl路径无法直接使用。例如:http://IP:端口/wsdl/IPacsServices?wsdl直接使用无法访问到对应的接口,而应当替换成soap中转换的url(http://IP:端口/soap/IPacsServices?wsdl)

 

 

  • 12
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Apache CXF是一个开源的WebService框架,可以帮助用户快速、简便地开发和部署WebService应用程序。它提供了多种方式调用WebService接口,下面介绍几种常用的方式: 1. 使用JAX-WS API:CXF实现了JAX-WS API,可以直接使用JAX-WS提供的API来调用WebService示例代码如下: ```java HelloWorldService service = new HelloWorldService(); HelloWorld port = service.getHelloWorldPort(); String result = port.sayHello("CXF"); ``` 2. 使用代理方式:CXF可以根据WebService WSDL文件自动生成代理类,然后通过调用代理类的方法来调用WebService接口示例代码如下: ```java JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean(); factory.setServiceClass(HelloWorld.class); factory.setAddress("http://localhost:8080/HelloWorld"); HelloWorld client = (HelloWorld) factory.create(); String result = client.sayHello("CXF"); ``` 3. 使用Spring配置文件:CXF提供了Spring配置文件方式来实现WebService接口调用。用户可以在Spring配置文件中配置WebService客户端,然后通过Spring容器来获取WebService客户端实例。示例代码如下: ```xml <jaxws:client id="helloClient" serviceClass="com.example.HelloWorld" address="http://localhost:8080/HelloWorld"/> ``` ```java ApplicationContext context = new ClassPathXmlApplicationContext("spring-config.xml"); HelloWorld client = (HelloWorld) context.getBean("helloClient"); String result = client.sayHello("CXF"); ``` 以上是几种常用的调用WebService接口方式,可以根据具体情况选择适合自己的方式
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值