用Soap消息调用Web Services(续)

简介:
    上篇《用Soap消息调用Web Services》只是简单的调用一个返回值为String的无参数WebService,这次改成调用一个参数为int型的返回值为一个类对象的WebService
 
服务器端WebService:
public class user implements Serializable
{
    private String name;    
    public user()
    {
        
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    
}

public class classDemo implements Serializable
{
    private user[] users;

    public user[] getUsers() {
        return users;
    }

    public void setUsers(user[] users) {
        this.users = users;
    }
}

public class HelloWorldServiceImpl implements IHelloWorldService 
{

    public  classDemo GetUsersInRoom(int rID)
    {
        classDemo cd = new classDemo();
        user[] a = new user[2];
        a[0] = new user();
        a[0].setName("aa");
        a[1] = new user();
        a[1].setName("bb");
        cd.setUsers(a);
        return cd;
    }
    
}

客户端代码:
public static void doSoapPost()
    {
        try 
        {
             //First create the connection
             SOAPConnectionFactory soapConnFactory = 
                                SOAPConnectionFactory.newInstance();
             SOAPConnection connection = 
                                 soapConnFactory.createConnection();
             
             //Next, create the actual message
             MessageFactory messageFactory = MessageFactory.newInstance();
             SOAPMessage message = messageFactory.createMessage();
             
             //Create objects for the message parts            
             SOAPPart soapPart = message.getSOAPPart();
             SOAPEnvelope envelope = soapPart.getEnvelope();
             SOAPBody body = envelope.getBody();       
             
            //Populate the Message
            StreamSource preppedMsgSrc = new StreamSource( 
                     new FileInputStream("E:\\soap.msg"));
            soapPart.setContent(preppedMsgSrc);
             //Save the message
             message.saveChanges();
             //Check the input
             System.out.println("\nREQUEST:\n");
             message.writeTo(System.out);
             System.out.println();
            //Send the message and get a reply   
                
            //Set the destination
            String destination = 
                  "http://localhost:8080/HelloWorld/services/HelloWorldService";
            //Send the message
            SOAPMessage reply = connection.call(message, destination);
            
//          Check the output
            System.out.println("\nRESPONSE:\n");
            //Create the transformer
            TransformerFactory transformerFactory = 
                               TransformerFactory.newInstance();
            Transformer transformer = 
                            transformerFactory.newTransformer();
            //Extract the content of the reply
            Source sourceContent = reply.getSOAPPart().getContent();
            //Set the output for the transformation
            StreamResult result = new StreamResult(System.out);
            
            transformer.transform(sourceContent, result);
            System.out.println();
             //Close the connection            
             connection.close();
        } 
        catch(Exception e) 
        {
                System.out.println(e.getMessage());
        }   
    }

客户端Soap请求格式:
REQUEST:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<ns1:GetUsersInRoom xmlns:ns1="http://phinecos.cnblogs.com">
<in0 xsi:type='xsd:int'>3
</in0> 
</ns1:GetUsersInRoom>
</soap:Body>
</soap:Envelope>

服务器端响应结果:
REQUEST:
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<ns1:GetUsersInRoom xmlns:ns1="http://phinecos.cnblogs.com">
<in0 xsi:type='xsd:int'>3
</in0> 
</ns1:GetUsersInRoom>
</soap:Body>
</soap:Envelope>

服务器端响应结果:

RESPONSE:
<?xml version="1.0" encoding="UTF-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><soap:Body><ns1:GetUsersInRoomResponse xmlns:ns1="http://phinecos.cnblogs.com"><ns1:out><users xmlns="http://boomga.com"><user><name>aa</name><path>/a.w3d</path></user><user><name>bb</name><path>/b.w3d</path></user></users></ns1:out></ns1:GetUsersInRoomResponse></soap:Body></soap:Envelope>


本文转自Phinecos(洞庭散人)博客园博客,原文链接http://www.cnblogs.com/phinecos/archive/2007/08/21/864260.html,如需转载请自行联系原作者
目录
相关文章
|
前端开发 JavaScript API
阿里云智能媒体服务IMS(Intelligent Media Services)的视频剪辑Web SDK
【1月更文挑战第15天】【1月更文挑战第72篇】阿里云智能媒体服务IMS(Intelligent Media Services)的视频剪辑Web SDK
515 6
|
XML JSON API
Understanding RESTful API and Web Services: Key Differences and Use Cases
在现代软件开发中,RESTful API和Web服务均用于实现系统间通信,但各有特点。RESTful API遵循REST原则,主要使用HTTP/HTTPS协议,数据格式多为JSON或XML,适用于无状态通信;而Web服务包括SOAP和REST,常用于基于网络的API,采用标准化方法如WSDL或OpenAPI。理解两者区别有助于选择适合应用需求的解决方案,构建高效、可扩展的应用程序。
|
XML 安全 PHP
PHP与SOAP Web服务开发:基础与进阶教程
本文介绍了PHP与SOAP Web服务的基础和进阶知识,涵盖SOAP的基本概念、PHP中的SoapServer和SoapClient类的使用方法,以及服务端和客户端的开发示例。此外,还探讨了安全性、性能优化等高级主题,帮助开发者掌握更高效的Web服务开发技巧。
|
XML Java 网络架构
使用 Spring Boot 公开 SOAP Web 服务端点:详细指南
使用 Spring Boot 公开 SOAP Web 服务端点:详细指南
1745 0
|
XML 关系型数据库 MySQL
Web Services 服务 是不是过时了?创建 Web Services 服务实例
本文讨论了WebServices(基于SOAP协议)与WebAPI(基于RESTful)在开发中的应用,回顾了WebServices的历史特点,比较了两者在技术栈、轻量化和适用场景的差异,并分享了使用VB.net开发WebServices的具体配置步骤和疑问。
434 0
|
XML 前端开发 JavaScript
RESTful Web Services
RESTful Web Services
158 2
|
XML 网络协议 Java
XML Web 服务技术解析:WSDL 与 SOAP 原理、应用案例一览
XML Web服务是基于WSDL、SOAP、RDF和RSS等标准的网络应用程序组件技术。WSDL描述服务接口和消息格式,SOAP用于结构化信息交换,RDF描述网络资源,RSS则用于发布网站更新。Web服务特点是自包含、自描述,基于开放协议,可重用且能连接现有软件。WSDL文档包含`types`、`message`、`portType`和`binding`元素,定义服务操作和协议。SOAP协议规定消息格式,通过HTTP等传输。
780 1
|
Ubuntu Linux 网络安全
在Amazon Web Services中使用R语言运行模拟
在Amazon Web Services中使用R语言运行模拟
|
XML 开发框架 JSON
探索 SOAP:揭开 Web 服务的神秘面纱(上)
探索 SOAP:揭开 Web 服务的神秘面纱(上)
|
XML 安全 数据安全/隐私保护
探索 SOAP:揭开 Web 服务的神秘面纱(下)
探索 SOAP:揭开 Web 服务的神秘面纱(下)