推薦:淺析8月編程語(yǔ)言榜:.Net前景如何?Tiobe近日公布了2009年8月的編程語(yǔ)言排行榜,51CTO注意到本期榜單中有Objective-CHE第一次進(jìn)入前二十名并取代之前的Lua,而Java、C、C 依然以絕對(duì)優(yōu)勢(shì)占據(jù)前三甲的位置。本期我們將重點(diǎn)關(guān)注微軟的.NET平臺(tái),以及新近推出的F#、Small Basic語(yǔ)言。 C# 4.0借Vis
最近一個(gè)項(xiàng)目由“WinForm直接訪(fǎng)問(wèn)DB2”移植到“WinForm通過(guò)PHP Web Service來(lái)訪(fǎng)問(wèn)DB2”。
(優(yōu)點(diǎn)是php可以架在Linux上,而Linux是免費(fèi)的)
這個(gè)命題的難點(diǎn)不是訪(fǎng)問(wèn)DB2,而是.NET調(diào)用PHP的Web Service。對(duì)于我這個(gè)長(zhǎng)期作.NET,之前一直以為只有.NET才可以做Web Service……的人來(lái)說(shuō),真是有點(diǎn)強(qiáng)“聰”所難了。
但是問(wèn)題還是要解決的,期限就擺在眼前呢。經(jīng)過(guò)一番調(diào)查,終于有了眉目,現(xiàn)在分享給大家。
首先要說(shuō)明的,PHP服務(wù)器需要至少需要兩個(gè)文件——一個(gè)WSDL文件和一個(gè)PHP文件。WSDL文件是一種機(jī)讀的XML文件,用于描述WebService提供的服務(wù)和調(diào)用方法(對(duì)于.NET則可以自動(dòng)生成調(diào)用代碼,十分好用),php文件就是真正實(shí)現(xiàn)的WEB服務(wù)了。
1)PHP服務(wù)器端代碼
1-1)TestWebService.php代碼
|
以下為引用的內(nèi)容:
<?php
class TestWebService
{
public function HelloWorld()
{
return array("HelloWorldResult"=>"Hello");
}
public function GetArray($args)
{
/*
注意,Web Service的方法在聲明時(shí)至多一個(gè)參數(shù),
可是在調(diào)用該方法時(shí)就必須傳value1,value2兩個(gè)參數(shù)。
(這一點(diǎn)十分令人費(fèi)解,我的理解是,在調(diào)用該方法時(shí),系統(tǒng)把所有參數(shù)都放到一個(gè)對(duì)象里傳過(guò)來(lái)的)
*/
$value1 = $args->value1;
$value2 = $args->value2;//這兩句是獲取真正的參數(shù)
$arry = array($value1,$value2);
//返回值也很特別,不是直接返回$arry,而是把它放到一個(gè)對(duì)象里再返回。
return array("GetArrayResult"=>$arry);
}
}
//創(chuàng)建WebSevice實(shí)例
$server = new SoapServer("TestWebService.wsdl");
//指定類(lèi)名
$server->setClass("TestWebService");
$server->handle();
?>
|
1-2)TestWebService.wsdl代碼
|
以下為引用的內(nèi)容:
<?xml version="1.0" encoding="utf-8"?>
<wsdl:definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tm="http://microsoft.com/wsdl/mime/textMatching/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:mime="http://schemas.xmlsoap.org/wsdl/mime/" xmlns:tns="http://tempuri.org/" xmlns:s="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://schemas.xmlsoap.org/wsdl/soap12/" xmlns:http="http://schemas.xmlsoap.org/wsdl/http/" targetNamespace="http://tempuri.org/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/">
<wsdl:types>
<s:schema elementFormDefault="qualified" targetNamespace="http://tempuri.org/">
<s:element name="HelloWorld">
<s:complexType />
</s:element>
<s:element name="HelloWorldResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="HelloWorldResult" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="GetArray">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="value1" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="value2" type="s:string" />
</s:sequence>
</s:complexType>
</s:element>
<s:element name="GetArrayResponse">
<s:complexType>
<s:sequence>
<s:element minOccurs="0" maxOccurs="1" name="GetArrayResult" type="tns:ArrayOfString" />
</s:sequence>
</s:complexType>
</s:element>
<s:complexType name="ArrayOfString">
<s:sequence>
<s:element minOccurs="0" maxOccurs="unbounded" name="string" nillable="true" type="s:string" />
</s:sequence>
</s:complexType>
</s:schema>
</wsdl:types>
<wsdl:message name="HelloWorldSoapIn">
<wsdl:part name="parameters" element="tns:HelloWorld" />
</wsdl:message>
<wsdl:message name="HelloWorldSoapOut">
<wsdl:part name="parameters" element="tns:HelloWorldResponse" />
</wsdl:message>
<wsdl:message name="GetArraySoapIn">
<wsdl:part name="parameters" element="tns:GetArray" />
</wsdl:message>
<wsdl:message name="GetArraySoapOut">
<wsdl:part name="parameters" element="tns:GetArrayResponse" />
</wsdl:message>
<wsdl:portType name="TestWebServiceSoap">
<wsdl:operation name="HelloWorld">
<wsdl:input message="tns:HelloWorldSoapIn" />
<wsdl:output message="tns:HelloWorldSoapOut" />
</wsdl:operation>
<wsdl:operation name="GetArray">
<wsdl:input message="tns:GetArraySoapIn" />
<wsdl:output message="tns:GetArraySoapOut" />
</wsdl:operation>
</wsdl:portType>
<wsdl:binding name="TestWebServiceSoap" type="tns:TestWebServiceSoap">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="HelloWorld">
<soap:operation soapAction="http://tempuri.org/HelloWorld" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="GetArray">
<soap:operation soapAction="http://tempuri.org/GetArray" style="document" />
<wsdl:input>
<soap:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:binding name="TestWebServiceSoap12" type="tns:TestWebServiceSoap">
<soap12:binding transport="http://schemas.xmlsoap.org/soap/http" />
<wsdl:operation name="HelloWorld">
<soap12:operation soapAction="http://tempuri.org/HelloWorld" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
<wsdl:operation name="GetArray">
<soap12:operation soapAction="http://tempuri.org/GetArray" style="document" />
<wsdl:input>
<soap12:body use="literal" />
</wsdl:input>
<wsdl:output>
<soap12:body use="literal" />
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="TestWebService">
<wsdl:port name="TestWebServiceSoap" binding="tns:TestWebServiceSoap">
<soap:address location="http://localhost/phpmyadmin/ws/TestWebService.php" />
</wsdl:port>
<wsdl:port name="TestWebServiceSoap12" binding="tns:TestWebServiceSoap12">
<soap12:address location="http://localhost/phpmyadmin/ws/TestWebService.php" />
</wsdl:port>
</wsdl:service>
</wsdl:definitions>
|
WSDL的代碼比較長(zhǎng),當(dāng)方法很多時(shí),手敲代碼是不太可能的。有一個(gè)巧的辦法,就是也用.NET實(shí)現(xiàn)一個(gè)不含真正方法體的Web Serivce,然后通過(guò)http://***/TestWebService.asmx?wsdl的方法生成wsdl代碼文件。
關(guān)于WSDL文件,我要說(shuō)明特別說(shuō)明兩點(diǎn):
(1)soap:address結(jié)點(diǎn)是聲明WebService的地址,在部署時(shí)要改成相應(yīng)地址;
(2)一維數(shù)組的聲明類(lèi)型為ArrayOfType,字符串?dāng)?shù)組為ArrayOfString。如果Type不是簡(jiǎn)單類(lèi)型,則Type需要另外聲明。
2).NET客戶(hù)端代碼
先要添加Web引用,地址為WSDL文件的Http地址。
調(diào)用代碼(C#)
|
以下為引用的內(nèi)容:
//初始化WebService
localhost.TestWebService srv = new localhost.TestWebService();
//調(diào)第一個(gè)方法
string str = srv.HelloWorld();
//調(diào)第二個(gè)方法
string[] arry= srv.GetArray("string1","string2");
|
總結(jié): (一)PHP是一種弱類(lèi)型語(yǔ)言,檢查錯(cuò)誤比較困難。array類(lèi)型也與一般理解的數(shù)組不同,它也有類(lèi)似Hashtable的用法。
(二)PHP Web Service方法的傳入?yún)?shù)、返回值都至多有一個(gè),因?yàn)檎嬲{(diào)用時(shí)的參數(shù)和返回值,都是包裝到一個(gè)對(duì)象中傳送的。
(三)PHP Web Service也支持自定義類(lèi)型和自定義類(lèi)型數(shù)組等復(fù)雜類(lèi)型,但不支持多組數(shù)組。
(四)若返回值需要是多張二維表時(shí),我淺薄的以為,可以傳化一組字符串?dāng)?shù)組傳送,格式為
[表1行數(shù)],[表1列數(shù)],[表1列名1],[表1列名2],……[表1列名N],[表1中按行列存放的值]
[表2行數(shù)],[表2列數(shù)],[表2列名1],[表2列名2],……[表2列名N],[表2中按行列存放的值]
……
[表M行數(shù)],[表M列數(shù)],[表M列名1],[表M列名2],……[表M列名N],[表2中按行列存放的值]
按順序?qū)⑸厦妫郏葜械膬?nèi)容串成字符串?dāng)?shù)組,效率還不錯(cuò),我測(cè)試10000行240列的數(shù)據(jù),我有現(xiàn)成編解代碼,有興趣的可以向我索取.
分享:怎樣在 C# 中發(fā)起會(huì)議之類(lèi)的特殊郵件從C#中調(diào)用Outlook中的API,可以發(fā)起會(huì)議之類(lèi)的特殊郵件。方法如下: 創(chuàng)建項(xiàng)目后,為它添加.NET引用: