java调用Webservice soap 接口

18 篇文章 1 订阅

今天需要对接一个接口,接口是遵循WebService协议的,然后我就懵逼了,没用过。于是就去百度了好久。于是就有了今天的博客。

Webservice是基于SOAP协议的请求,SOAP协议是xml格式的。所以请求参数和回执全部都是xml。

获取命名空间和参数等信息

一般webservice接口只会给一个xxx.wsdl文件

如:http://www.webxml.com.cn/WebServices/IpAddressSearchWebService.asmx?wsdl

从这个文件中我们可以获取到接口所需的参数,我们可以使用soapUI软件来进行查看

然后新增一个项目

image-20210326184856503

名称随便写 wsdl就写url 点击ok

image-20210326184926959

然后点击里面的Request1就可以看到参数信息了,其中有个?号,我们到时候把?替换成xml格式的参数就可以发送了。

image-20210326185019449

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ser="http://www.cvicse.com/service/">
   <soapenv:Header/>
   <soapenv:Body>
      <ser:syncServiceStationOperationRequest>
         <in></in>
      </ser:syncServiceStationOperationRequest>
   </soapenv:Body>
</soapenv:Envelope>

请求

pom文件

 <dependency>
        <groupId>org.apache.ws.commons.axiom</groupId>
        <artifactId>axiom-api</artifactId>
        <version>1.2.4</version>
    </dependency>
    <dependency>
        <groupId>org.apache.ws.commons.axiom</groupId>
        <artifactId>axiom-impl</artifactId>
        <version>1.2.4</version>
    </dependency>
    <dependency>
        <groupId>wsdl4j</groupId>
        <artifactId>wsdl4j</artifactId>
        <version>1.6.3</version>
    </dependency>
    <dependency>
        <groupId>org.apache.ws.xmlschema</groupId>
        <artifactId>xmlschema-core</artifactId>
        <version>2.2.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.5</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpcore</artifactId>
        <version>4.4.1</version>
    </dependency>
    <dependency>
        <groupId>commons-httpclient</groupId>
        <artifactId>commons-httpclient</artifactId>
        <version>3.1</version>
    </dependency>

请求

    public static void main(String[] args) throws Exception {
        String str =new String("<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:ser=\"http://www.cvicse.com/service/\"><soapenv:Header/><soapenv:Body><ser:syncServiceStationOperationRequest><in><![CDATA[这里放入xml参数]]></in></ser:syncServiceStationOperationRequest></soapenv:Body></soapenv:Envelope>".getBytes(),"UTF-8");
        System.out.println(str);
        HttpClient httpClient = new HttpClient();
        httpClient.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT,3000);
        httpClient.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, 1000);
        PostMethod post = new PostMethod("http://xxxxxx/services/syncServiceStation?wsdl");
        post.setRequestHeader("Content-Type","text/xml;charset=utf-8");
        RequestEntity re = new InputStreamRequestEntity(new ByteArrayInputStream(str.getBytes()));
        post.setRequestEntity(re);
        BufferedReader in = null;
        try {
            int status =httpClient.executeMethod(post);
            if (status == 200) {
                System.out.println("成功");
                InputStream io  =post.getResponseBodyAsStream();
                InputStreamReader is = new InputStreamReader(io, "UTF-8");
                in = new BufferedReader(is);
                String inputLine;
                StringBuilder result = new StringBuilder();
                while ((inputLine = in.readLine()) != null)
                {
                    result.append(inputLine);
                }
                String a =result.toString().replace("&lt;", "<").replace("&gt;", ">");
                System.out.println(a);
            } else {
                System.out.println("失败");
                System.out.println(post.getResponseBodyAsString());
            }

        } catch (IOException e) {

        } finally {
            if (in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            post.releaseConnection();
            httpClient.getHttpConnectionManager().closeIdleConnections(0);
        }
    }

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Java可以通过SOAP协议调用Web Service接口。具体步骤如下: 1. 创建一个SOAP消息请求体,包括SOAP消息头和SOAP消息体。 2. 创建一个SOAP连接对象,并设置连接的URL地址。 3. 发送SOAP消息请求,等待Web Service接口返回SOAP消息响应。 4. 解析SOAP消息响应,获取需要的数据。 5. 关闭SOAP连接对象。 需要注意的是,调用Web Service接口需要提供接口的WSDL文件,以便生成客户端代码。可以使用Java自带的wsimport工具生成客户端代码,也可以使用第三方工具如Apache CXF等。 另外,调用Web Service接口时需要注意SOAP消息的格式和命名空间等问题,以确保请求和响应能够正确解析。 ### 回答2: Java是一门广泛应用于开发Web应用程序的高级编程语言。它是一门面向对象的语言,具备简单易学、可移植性强、安全性高等特点,被广泛地应用于各种软件开发领域Java调用WebService接口,是现在Web开发非常重要的技术之一。 WebService(简称WS)是基于Web的远程调用协议,用于服务提供者和服务消费者之间的互操作。它是一种跨平台、跨语言的通信方式,其SOAP(简单对象访问协议)是一种常用的WebService交互协议。Java可以通过SOAP调用WebService接口服务。 Java使用SOAP调用WebService接口的步骤如下: 1.编写WebService客户端代码 Java提供了访问WebService接口服务的类:javax.xml.soap.SOAPConnection和javax.xml.soap.SOAPConnectionFactory。我们可以通过下面的代码获取SOAPConnection: ``` SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance(); SOAPConnection connection = factory.createConnection(); ``` 2.创建SOAP请求 在SOAP请求,我们需要设置相关的参数,包括:SOAP消息的版本、SOAP请求消息体、SOAP请求头等。下面是创建请求消息的代码示例: ``` //初始化一个SOAP消息 MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL); SOAPMessage soapMessage = messageFactory.createMessage(); //创建SOAP报文头 SOAPHeader soapHeader = soapMessage.getSOAPHeader(); SOAPBody soapBody = soapMessage.getSOAPBody(); ``` 3.设置SOAP请求参数 在请求,需要设置相关参数,包括EndPoint地址、SOAPAction、参数值等。下面是设置请求参数的代码示例: ``` //设置WebService的请求地址 URL url= new URL("http://www.example.com/WebService"); //创建一个SOAP消息并设置Header和Body SOAPMessage soapMessage = MessageFactory.newInstance().createMessage(); SOAPHeader soapHeader = soapMessage.getSOAPHeader(); SOAPBody soapBody = soapMessage.getSOAPBody(); //设置要调用WebService方法名 String methodName = "getWebServiceResult"; //设置SoapAction String soapAction = "http://www.example.com/WebService/getWebServiceResult"; soapMessage.getMimeHeaders().addHeader("SOAPAction", soapAction); //调用WebService SOAPConnectionFactory factory = SOAPConnectionFactory.newInstance(); SOAPConnection connection = factory.createConnection(); SOAPMessage response = connection.call(soapMessage, url); ``` 4.解析SOAP响应 在获取WebService接口服务的结果后,我们需要对SOAP响应进行解析。下面是解析SOAP响应的代码示例: ``` //解析响应数据 SOAPBody soapBody = response.getSOAPBody(); NodeList nodeList = soapBody.getElementsByTagName("return"); String responseStr = nodeList.item(0).getTextContent();//获取返回值 ``` 综上所述,Java调用WebService接口SOAP的过程包括了编写WebService客户端代码、创建SOAP请求、设置SOAP请求参数及解析SOAP响应等步骤。这些步骤的实现可以帮助我们更好地调用WebService接口,并获取接口的响应结果。 ### 回答3: Java 语言作为一种广泛应用于企业级应用的编程语言,自然对接webservice等网络接口是其常见的应用场景之一。在Java调用webservice接口有多种方法,其最常用的是使用soap协议来调用webservice接口soap协议即简单对象访问协议,是一种基于XML的协议,用于将网络上的消息传递给远程执行的程序。使用soap协议调用webservice接口可以实现跨平台、跨语言、跨网络的消息传递,是一种比较常用的通信方式。 在Java调用webservice接口需要使用Java API for XML Web Services(JAX-WS)技术。JAX-WS是一种Java EE标准,提供了一套API,支持Java应用与webservice接口之间的通信。以下是调用webservice接口的步骤: 1. 创建一个JAX-WS客户端工程(Client Project),该工程用于调用webservice接口。可以在Eclipse、IntelliJ IDEA等IDE创建该工程。 2. 在工程创建一个webservice客户端代理(Web Service Client Proxy),用于调用webservice接口。可以使用Eclipse这类IDE提供的WSDL文件生成工具(例如wsimport)来生成webservice客户端代理。 3. 使用生成的webservice客户端代理来调用webservice接口。在Java,使用webservice客户端代理对象调用webservice接口的过程类似于使用Java接口调用远程程序,可以通过SOAPMessage传输请求和响应信息,完成与webservice接口进行交互的过程。 需要注意的是,在调用webservice接口时,需要提供webservice接口的URL、命名空间(Namespace)、操作名称(Operation)等信息。另外,我们还可以为webservice接口提供请求头信息(Header)和请求参数(Parameter),以便webservice端正确地处理请求。 总之,通过Java调用webservice接口,在很多应用都是必不可少的一步,特别是在企业级应用更是如此。因此,我们需要深入理解soap协议和JAX-WS技术的原理和应用,才能在实际开发进行合理的使用和优化。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值