有很长一段时间没有写博客了。最近一年,收到不少交流邮件都是关于webservice,大部分都是素要webservice实例。
其实对webservice 了解不是很深,只是根据工作需要去接触,去学习。最近项目和其他一个.net项目需要做数据交互,交互方式就采用Webservice.
特将开发中遇到的问题总结并分享;
开发中遇到的问题有:
1)java和.net 的默认namespace问题;
2)两者产生的SOAP文件格式不一致,有三种,Microsoft的;IBM的,通过抓包工具可以获得java产生的Soap消息
3)入参的数据类型会影响调用的正确性;
环境说明
a.java作为web service 的客户端去调用 .Net Web Service的服务端;
b.采用 axis 版本1.4;
c..net webservice 采用Microsoft Visual Studio 2008开发;
1. .Net webservice 服务端
.net 提供的webservice 链接:http://******/WebService/AsRptToStdService.asmx;
其中采用Soap 作为消息交互。
信息如下:
Request/请求:
POST /globalequitydata/WebService/AsRptToStdService.asmx HTTP/1.1
Host: szeqdatabeta0.morningstar.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/AsRptToStd"
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<AsRptToStd xmlns="http://tempuri.org/">
<strCID>string</strCID>
<longRowFileDocumentId>long</longRowFileDocumentId>
<strAsRptFilePath>string</strAsRptFilePath>
</AsRptToStd>
</soap:Body>
</soap:Envelope>
Response/响应
HTTP/1.1 200 OK
Content-Type: text/xml; charset=utf-8
Content-Length: length
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<AsRptToStdResponse xmlns="http://tempuri.org/">
<AsRptToStdResult>boolean</AsRptToStdResult>
</AsRptToStdResponse>
</soap:Body>
</soap:Envelope>
2.Java webservice 客户端
直接贴代码
从代码中可以看出,该webservice是传入三个参数,返回一个Boolean型的结果;
执行代码,通过抓包工具,可以看到生成的xml文件。红色部分即是客服端Request的SOAP消息,传输至服务端,服务端response SOAP至客服端(绿色标志的)
下面是通过抓包工具获取的数据:
POST /globalequitydata/WebService/AsRptToStdService.asmx HTTP/1.0
Content-Type: text/xml; charset=utf-8
Accept: application/soap+xml, application/dime, multipart/related, text/*
User-Agent: Axis/1.4
Host: szeqdatabeta0.morningstar.com
Cache-Control: no-cache
Pragma: no-cache
SOAPAction: "http://tempuri.org/AsRptToStd"
Content-Length: 658
HTTP/1.1 200 OK
Connection: close
Date: Tue, 01 Jun 2010 03:01:30 GMT
Server: Microsoft-IIS/6.0
X-Powered-By: ASP.NET
X-AspNet-Version: 2.0.50727
Cache-Control: private, max-age=0
Content-Type: text/xml; charset=utf-8
Content-Length: 356
<?xml version="1.0" encoding="utf-8"?><soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><AsRptToStdResponse xmlns="http://tempuri.org/"><AsRptToStdResult>true</AsRptToStdResult></AsRptToStdResponse></soap:Body></soap:Envelope>
我们也可以不用axis ,而自己组装SOAP消息,发送调用webservice;使用axis可以为我们自动组装soap消息;
3.遇到的问题
下面来解答开发过程中遇到的主要问题;
1)java和.net 的默认namespace问题;
java:http://tempuri.org
.net :http://tempuri.org/
区别就是符号“/”;如果不带“/”产生的结果会出错;
问题解决:统一加上“/”;
2)两者产生的SOAP文件格式不一致,有三种,Microsoft的;IBM的,通过抓包工具可以获得java产生的Soap消息
SOAP 消息头问题,从上面抓包的信息可以看出:请求的是<soapenv:Envelope 而response的是 <soap:Envelope
这是由于不同的服务商封装成的不同格式的soap ,但都是遵循SOAP,可以通用;
Microsoft的是 <soap:Envelope ,而IBM的是 <soapenv:Envelope ;
如下两种格式的SOAP是一致的:
3)入参的数据类型会影响调用的正确性;
第二个参数的入参是long型。如果修改make1方法的String 型成 long 型,则程序异常;
AxisFault
faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Client
faultSubcode:
faultString: System.Web.Services.Protocols.SoapException: Server was unable to read request. ---> System.InvalidOperationException: There is an error in XML document (1, 440). ---> System.FormatException: Input string was not in a correct format.
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseInt64(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.Xml.XmlConvert.ToInt64(String s)
其实我们只需要在参数设置里面设成org.apache.axis.encoding.XMLType.XSD_LONG;
作者:南极光
时间:2010-6-1
欢迎大家同我联系,wayfoon@163.com
欢迎转载,转载请保留申明信息。