解析.NET調PHP Web Service的典型例子_.Net教程

      編輯Tag賺U幣
      教程Tag:暫無Tag,歡迎添加,賺取U幣!

      推薦:淺析8月編程語言榜:.Net前景如何?
      Tiobe近日公布了2009年8月的編程語言排行榜,51CTO注意到本期榜單中有Objective-CHE第一次進入前二十名并取代之前的Lua,而Java、C、C 依然以絕對優勢占據前三甲的位置。本期我們將重點關注微軟的.NET平臺,以及新近推出的F#、Small Basic語言。 C# 4.0借Vis

      最近一個項目由“WinForm直接訪問DB2”移植到“WinForm通過PHP Web Service來訪問DB2”。

      (優點是php可以架在Linux上,而Linux是免費的)

      這個命題的難點不是訪問DB2,而是.NET調用PHP的Web Service。對于我這個長期作.NET,之前一直以為只有.NET才可以做Web Service……的人來說,真是有點強“聰”所難了。

      但是問題還是要解決的,期限就擺在眼前呢。經過一番調查,終于有了眉目,現在分享給大家。

      首先要說明的,PHP服務器需要至少需要兩個文件——一個WSDL文件和一個PHP文件。WSDL文件是一種機讀的XML文件,用于描述WebService提供的服務和調用方法(對于.NET則可以自動生成調用代碼,十分好用),php文件就是真正實現的WEB服務了。

      1)PHP服務器端代碼

      1-1)TestWebService.php代碼

      以下為引用的內容:

      <?php
      class TestWebService
      {
          
      public function HelloWorld()
          {
              
      return array("HelloWorldResult"=>"Hello");
          }

          
      public function GetArray($args)
              {
                
      /*
                 注意,Web Service的方法在聲明時至多一個參數,
                  可是在調用該方法時就必須傳value1,value2兩個參數。
                  (這一點十分令人費解,我的理解是,在調用該方法時,系統把所有參數都放到一個對象里傳過來的)
                
      */

              
      $value1 = $args->value1;  
              
      $value2 = $args->value2;//這兩句是獲取真正的參數
       
              
      $arry = array($value1,$value2);

              
      //返回值也很特別,不是直接返回$arry,而是把它放到一個對象里再返回。
              return array("GetArrayResult"=>$arry);
          }
      }

      //創建WebSevice實例
      $server = new SoapServer("TestWebService.wsdl");
      //指定類名
      $server->setClass("TestWebService");

      $server->handle();


      ?>

      1-2)TestWebService.wsdl代碼

      以下為引用的內容:

      <?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的代碼比較長,當方法很多時,手敲代碼是不太可能的。有一個巧的辦法,就是也用.NET實現一個不含真正方法體的Web Serivce,然后通過http://***/TestWebService.asmx?wsdl的方法生成wsdl代碼文件。

      關于WSDL文件,我要說明特別說明兩點:

      (1)soap:address結點是聲明WebService的地址,在部署時要改成相應地址;

      (2)一維數組的聲明類型為ArrayOfType,字符串數組為ArrayOfString。如果Type不是簡單類型,則Type需要另外聲明。

      2).NET客戶端代碼

      先要添加Web引用,地址為WSDL文件的Http地址。

      調用代碼(C#)

      以下為引用的內容:

              //初始化WebService
              localhost.TestWebService srv = new localhost.TestWebService();
              
      //調第一個方法
               string str = srv.HelloWorld();
              
      //調第二個方法
               string[] arry= srv.GetArray("string1","string2");

      總結: (一)PHP是一種弱類型語言,檢查錯誤比較困難。array類型也與一般理解的數組不同,它也有類似Hashtable的用法。

      (二)PHP Web Service方法的傳入參數、返回值都至多有一個,因為真正調用時的參數和返回值,都是包裝到一個對象中傳送的。

      (三)PHP Web Service也支持自定義類型和自定義類型數組等復雜類型,但不支持多組數組。

      (四)若返回值需要是多張二維表時,我淺薄的以為,可以傳化一組字符串數組傳送,格式為

      [表1行數],[表1列數],[表1列名1],[表1列名2],……[表1列名N],[表1中按行列存放的值]

      [表2行數],[表2列數],[表2列名1],[表2列名2],……[表2列名N],[表2中按行列存放的值]

      ……

      [表M行數],[表M列數],[表M列名1],[表M列名2],……[表M列名N],[表2中按行列存放的值]

      按順序將上面[]中的內容串成字符串數組,效率還不錯,我測試10000行240列的數據,我有現成編解代碼,有興趣的可以向我索取.

      分享:怎樣在 C# 中發起會議之類的特殊郵件
      從C#中調用Outlook中的API,可以發起會議之類的特殊郵件。方法如下: 創建項目后,為它添加.NET引用:

      來源:模板無憂//所屬分類:.Net教程/更新時間:2009-08-25
      相關.Net教程