有很長一段時間沒有寫博客了。最近一年,收到不少交流郵件都是關於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
歡迎大家同我聯繫,[email protected]
歡迎轉載,轉載請保留申明信息。