Web Service难道又是一个美丽的童话?
作者: 萧百龄, 出处:IT专家网, 责任编辑: 王尔玉,
2007-09-28 11:20
当我们想要用一种平台去访问另一种平台的WS的时候,一个矛盾出现了,就是对WSML的解释存在差异性。作者做了一个例子,在VB中用MSSoap访问PHP Pear SOAP生成的WS……
Web Service用了这么多年了,相信没有人会怀疑其跨平台性。无论是MS的WS还是J2EE的WS,自己和自己平台的交互一般都不会有什么配置问题。可是,当我们想要用一种平台去访问另一种平台的WS的时候,一个矛盾出现了,就是对WSML的解释存在差异性。
我做了一个例子,在VB中用MSSoap访问PHP Pear SOAP生成的WS,众所周知WSDL一般都不是人写的。
假设有一个发送短消息的函数
PHP生成Web Service有三种主要的方法:
1 用自带的soap函数
| function send($address, $content, $key) { } $server = new SoapServer(null, array('urn' => "SmsSender")); $server->addFunction("send"); $server->handle(); |
这种方法无法生成wsdl
2 用Pear的SOAP
| class SmsSender { function send($address, $content, $key) { } } $server = new SOAP_Server(); $webservice = new SmsSender(); $server->addObjectMap($webservice,'urn:SmsSender'); $server->service($HTTP_RAW_POST_DATA); |
| <?xml version="1.0"?><definitions name="SmsSender" targetNamespace="urn:SmsSender" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="urn:SmsSender" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns="http://schemas.xmlsoap.org/wsdl/"> <types xmlns="http://schemas.xmlsoap.org/wsdl/"> </types> <portType name="SmsSenderPort" /> <binding name="SmsSenderBinding" type="tns:SmsSenderPort"> <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" /> </binding> <service name="SmsSenderService"> <documentation /> <port name="SmsSenderPort" binding="tns:SmsSenderBinding"> <soap:address location="http://202.195.160.145/sms/ws.php" /> </port> </service> </definitions> |
3 使用nusoap,不过由于最新版本有bug,所以我懒得用。
下面我们用VB调用上面的第二个WS
| Dim client Set client = CreateObject("MSSOAP.SOAPClient30") client.ClientProperty("ServerHTTPRequest") = True On Error Resume Next Call client.MSSoapInit("http://host/sms/ws.php?wsdl", "SmsSenderService", "SmsSenderPort") If Err <> 0 Then Debug.Print "initialization failed" + Err.Description End If strg = client.send("12345678", "test", "key") If Err <> 0 Then Debug.Print Err.Description Debug.Print "faultcode=" + client.FaultCode Debug.Print "faultstring=" + client.FaultString Debug.Print "FaultActor=" + client.FaultActor Debug.Print "Detail=" + client.Detail End If |
| initialization failedWSDLPort:Port SmsSenderPort has no operations HRESULT=0x1: 函数不正确。 - WSDLService:Processing service SmsSenderService found no port definitions HRESULT=0x80070057: 参数不正确。 - WSDLReader:Analyzing the WSDL file failed HRESULT=0x80070057: 参数不正确。 - Client:One of the parameters supplied is invalid. HRESULT=0x80070057: 参数不正确。 |
那是不是WSDL的确错误呢?我继续用PHP的Pear的SOAP客户端来调用这个WS
| $client = new SOAP_Client("http://host/sms/ws.php"); $client->send("12345677", "test", "key"); |
我甚至没有指定WSDL,也能顺利的执行。
还有Axis的WS如果给.net调用,经常也会存在一些莫名奇妙的错误。
如果我用一种平台的工具生成WSDL,然后让另一种平台用这个WSDL反向生成代码,这种代码的可读性会大大下降。
问题的症结似乎在于WSDL的过度复杂和设计之初,没有考虑手写代码的便捷性和可行性,HTML就是一个完全可以手写的代码。
难道Web Servcie就和EJB一样,所谓的一次部署只是一个美丽的童话?各家厂商为了自己的利益,无法使得WSDL变得完全中立?
或者是否应该有一个第三方的工具,独立与任何语言的工具来定义WS,然后通过可定制的模板,自动生成各家语言的各种框架所需的配置文件?如果有的话,可能会火!
欢迎大家参与讨论,你的WS最佳实践是什么?
- 本文关键词:

