35
16 16 Web Service Web Service 第第第第第第 第第第第第第 16-1 Web Service 16-1 Web Service 第第第第第第 第第第第第第 16-2 SOAP 16-2 SOAP 第第第 第第第 16-3 HttpRequest 16-3 HttpRequest 第第第第 第第第第 SOAP SOAP 第第第第 第第第第 16-4 WSDL 16-4 WSDL 第第第 第第第 16-5 Google 16-5 Google 第第第第第 第第第第第 Web Service Web Service

第 16 章 Web Service應用程式架構

Embed Size (px)

DESCRIPTION

第 16 章 Web Service應用程式架構. 16-1 Web Service 應用程式架構 16-2 SOAP 的基礎 16-3 HttpRequest 物件建立 SOAP 應用程式 16-4 WSDL 的基礎 16-5 Google 搜尋服務的 Web Service. 16-1 Web Service應用程式架構. Web Service 的基礎 Web Service 應用程式架構. Web Service 的基礎 -1. Web Service 屬於一種企業級的應用程式,可以透過 Internet 建立自動機制提供資料交換和工作分享等資源共享的功能。 - PowerPoint PPT Presentation

Citation preview

Page 1: 第 16 章  Web Service應用程式架構

第第 1616 章 章 Web ServiceWeb Service應用程式應用程式架構架構

16-1 Web Service16-1 Web Service 應用程式架構應用程式架構 16-2 SOAP16-2 SOAP 的基礎的基礎 16-3 HttpRequest16-3 HttpRequest 物件建立物件建立 SOAPSOAP 應用程應用程

式式 16-4 WSDL16-4 WSDL 的基礎的基礎 16-5 Google16-5 Google 搜尋服務的搜尋服務的 Web ServiceWeb Service

Page 2: 第 16 章  Web Service應用程式架構

16-1 Web Service16-1 Web Service應用程式架應用程式架構構

Web ServiceWeb Service 的基礎的基礎 Web ServiceWeb Service 應用程式架構應用程式架構

Page 3: 第 16 章  Web Service應用程式架構

Web ServiceWeb Service 的基礎的基礎 -1-1

Web ServiceWeb Service 屬於一種企業級的應用程式,屬於一種企業級的應用程式,可以透過可以透過 InternetInternet 建立自動機制提供資料建立自動機制提供資料交換和工作分享等資源共享的功能。交換和工作分享等資源共享的功能。

Web ServiceWeb Service 使用簡單和公開標準的協定,使用簡單和公開標準的協定,提供低成本的軟體整合和資料分享,它是提供低成本的軟體整合和資料分享,它是一種可以在不同的作業系統,使用不同的一種可以在不同的作業系統,使用不同的程式語言開發的應用程式,能夠直接與位程式語言開發的應用程式,能夠直接與位在不同位置的其它應用程式進行通訊和資在不同位置的其它應用程式進行通訊和資料交換。料交換。

Page 4: 第 16 章  Web Service應用程式架構

Web ServiceWeb Service 的基礎的基礎 -2-2

Web ServiceWeb Service 應用程式提供一組通用服務,應用程式提供一組通用服務,可以同時提供多個用戶端可以同時提供多個用戶端 (Client)(Client) 使用,使用,這是一組使用這是一組使用 XMLXML 格式作為傳遞訊息的函格式作為傳遞訊息的函數呼叫,用戶端透過數呼叫,用戶端透過 HTTPHTTP 傳送函數呼叫傳送函數呼叫給伺服端,伺服端以給伺服端,伺服端以 HTTPHTTP 將結果傳回給將結果傳回給用戶端,它提供用戶端更多的彈性,因為用戶端,它提供用戶端更多的彈性,因為可以只使用所需的函數呼叫取得所需的資可以只使用所需的函數呼叫取得所需的資訊。訊。

Page 5: 第 16 章  Web Service應用程式架構

Web ServiceWeb Service 應用程式架構應用程式架構 -1-1

Web ServiceWeb Service 基本上是提供一組通用的遠基本上是提供一組通用的遠端函數呼叫端函數呼叫 (RPC(RPC,, Remote Procedure Remote Procedure Calls)Calls) ,為了用戶端和伺服端能夠依據相,為了用戶端和伺服端能夠依據相同的訊息格式和規格來傳遞資訊,我們需同的訊息格式和規格來傳遞資訊,我們需要一種標準的通訊協定,這就是「要一種標準的通訊協定,這就是「 SOASOAPP」」 (Simple Object Access Protocol)(Simple Object Access Protocol) ,,它是一種結合它是一種結合 XMLXML 格式的訊息和格式的訊息和 HTTPHTTP 協協定的通訊協定,使用定的通訊協定,使用 SOAPSOAP的的Web Web ServiceService 應用程式架構。應用程式架構。

Page 6: 第 16 章  Web Service應用程式架構

Web ServiceWeb Service 應用程式架構應用程式架構 -2-2

Page 7: 第 16 章  Web Service應用程式架構

16-2 SOAP16-2 SOAP 的基礎的基礎 什麼是什麼是 SOAPSOAP SOAPSOAP 訊息的基本語法訊息的基本語法 SOAPSOAP 訊息交換架構訊息交換架構

Page 8: 第 16 章  Web Service應用程式架構

什麼是什麼是 SOAP-1SOAP-1

「「 SOAPSOAP」」 (Simple Object Access Protocol)(Simple Object Access Protocol)是一種在分散系統交換資料的通訊協定,它屬於一是一種在分散系統交換資料的通訊協定,它屬於一種種 XMLXML 基礎的通訊協定,其傳送的資料就是基礎的通訊協定,其傳送的資料就是 XMLXML元素的資料。元素的資料。

SOAPSOAP 屬於一種標準屬於一種標準 HTTPHTTP 通訊協定的資料交換協通訊協定的資料交換協定,由定,由MicrosoftMicrosoft、、 DevelopMentorDevelopMentor、、 IBMIBM、、 Lotus Lotus Development Corp.Development Corp.、、 HPHP、、 UserLand UserLand SoftwareSoftware 等公司所共同製定,等公司所共同製定, SOAP 1.1SOAP 1.1 規格屬規格屬於微軟於微軟 Windows DNA 2000Windows DNA 2000 架構的主要元素,提架構的主要元素,提供分散式應用程式系統開發的資料交換。供分散式應用程式系統開發的資料交換。

Page 9: 第 16 章  Web Service應用程式架構

什麼是什麼是 SOAP-2SOAP-2

目前目前 InternetInternet 服務或瀏覽程式都是支援服務或瀏覽程式都是支援 HTTPHTTP ,,分散式分散式 InternetInternet 應用程式雖然能夠使用「應用程式雖然能夠使用「 RPRPCC 」」 (Remote Procedure Calls)(Remote Procedure Calls) 執行執行 DCOMDCOM物件,但是物件,但是 HTTPHTTP 通訊協定並不支援通訊協定並不支援 RPCRPC ,此時,此時的的 SOAPSOAP 通訊協定就可以解決這個問題,其特點通訊協定就可以解決這個問題,其特點如下所示:如下所示:• SOAPSOAP 是一種負載很輕的通訊協定,傳送的資料是文字是一種負載很輕的通訊協定,傳送的資料是文字

內容的內容的 XMLXML 文件。文件。• SOAPSOAP 屬於一種在應用程式間使用屬於一種在應用程式間使用 HTTPHTTP 通訊協傳遞通訊協傳遞

資料的協定。資料的協定。• SOAPSOAP 是一種是一種 XMLXML 基礎的技術,簡單而且擁有很好的基礎的技術,簡單而且擁有很好的

擴充性。擴充性。

Page 10: 第 16 章  Web Service應用程式架構

SOAPSOAP 訊息的基本語法訊息的基本語法 -1-1<SOAP-ENV:Envelope <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoSOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">ding/">

<SOAP-ENV:Header><SOAP-ENV:Header> <t:Transaction xmlns:t="http://www.company.com/transaction"<t:Transaction xmlns:t="http://www.company.com/transaction" SOAP-ENV:mustUnderstand="1">SOAP-ENV:mustUnderstand="1"> 55 </t:Transaction></t:Transaction></SOAP-ENV:Header></SOAP-ENV:Header><SOAP-ENV:Body><SOAP-ENV:Body> <m:getPrice xmlns:m="http://www.company.com/soap"><m:getPrice xmlns:m="http://www.company.com/soap"> <code>1234</code><code>1234</code> </m:getPrice></m:getPrice></SOAP-ENV:Body></SOAP-ENV:Body></SOAP-ENV:Envelope></SOAP-ENV:Envelope>

Page 11: 第 16 章  Web Service應用程式架構

SOAPSOAP 訊息的基本語法訊息的基本語法 -2-2

SOAPSOAP 的名稱空間的名稱空間 SOAPSOAP 應用程式產生的應用程式產生的 SOAPSOAP 訊息需要定義所有訊息需要定義所有

元素和屬性的名稱空間,而且元素和屬性的名稱空間,而且 SOAPSOAP 應用程式一應用程式一定要能夠處理收到訊息的名稱空間,定要能夠處理收到訊息的名稱空間, SOAPSOAP 擁有擁有兩個預設的名稱空間,如下所示:兩個預設的名稱空間,如下所示:• SOAP EnvelopeSOAP Envelope 預設的名稱空間,使用的字頭為預設的名稱空間,使用的字頭為【【 SOAP-ENVSOAP-ENV 】】,如下所示:,如下所示:

http://schemas.xmlsoap.org/soap/envelope/http://schemas.xmlsoap.org/soap/envelope/• SOAPSOAP 加碼和資料型態預設的名稱空間,如下所示:加碼和資料型態預設的名稱空間,如下所示:

http://schemas.xmlsoap.org/soap/encoding/http://schemas.xmlsoap.org/soap/encoding/

Page 12: 第 16 章  Web Service應用程式架構

SOAPSOAP 訊息的基本語法訊息的基本語法 -3-3

EnvelopeEnvelope 元素元素 EnvelopeEnvelope 元素為元素為 SOAPSOAP 訊息的根元訊息的根元

素,素, SOAPSOAP 訊息一定擁有此元素,如果元訊息一定擁有此元素,如果元素包含名稱空間宣告和額外的屬性,屬性素包含名稱空間宣告和額外的屬性,屬性也需要使用名稱空間的字頭。也需要使用名稱空間的字頭。

encodingStyleencodingStyle 屬性是一組屬性是一組 URIURI 用來定義用來定義SOAPSOAP 加碼和資料型態,這個屬性能夠出加碼和資料型態,這個屬性能夠出現在現在 SOAPSOAP 訊息的任何元素,如果在訊息的任何元素,如果在EnvelopeEnvelope 元素,此時所有的子元素都屬於元素,此時所有的子元素都屬於名稱空間的範圍。名稱空間的範圍。

Page 13: 第 16 章  Web Service應用程式架構

SOAPSOAP 訊息的基本語法訊息的基本語法 -4-4

HeaderHeader 元素元素 HeaderHeader 元素屬於元素屬於 SOAPSOAP 訊息的選項元素,如果訊息的選項元素,如果

出現,一定為出現,一定為 EnvelopeEnvelope 元素的子元素,這是一元素的子元素,這是一個彈性機制可以擴充個彈性機制可以擴充 SOAPSOAP 訊息成為一個分散式訊息成為一個分散式的模組,常常使用在授權、交易和付款管理。的模組,常常使用在授權、交易和付款管理。

HeaderHeader 元素的子元素一定需要宣告名稱空間和元素的子元素一定需要宣告名稱空間和字頭,可以使用兩個屬性,如下表所示:字頭,可以使用兩個屬性,如下表所示:屬性 說明actor 這個屬性值為 URI,在定義 Header元素的接受者

mustUnderstand 這個屬性值為 1或 0,指出 Header子元素為必須或選項元素

Page 14: 第 16 章  Web Service應用程式架構

SOAPSOAP 訊息的基本語法訊息的基本語法 -5-5

BodyBody 元素元素 BodyBody 元素屬於元素屬於 SOAPSOAP 訊息的主要元素,這是訊息的主要元素,這是

EnvelopeEnvelope 元素的子元素,如果擁有元素的子元素,如果擁有 HeaderHeader 元元素,素, BodyBody 元素必為元素必為 HeaderHeader 元素的下一個元素,元素的下一個元素,它的內容是傳送的訊息,可以包含它的內容是傳送的訊息,可以包含 RPCRPC 呼叫,如呼叫,如下所示:下所示:

<m:getPrice <m:getPrice xmlns:m="http://www.company.com/soapxmlns:m="http://www.company.com/soap">">

<code>1234</code><code>1234</code></m:getPrice></m:getPrice>

Page 15: 第 16 章  Web Service應用程式架構

SOAPSOAP 訊息交換架構訊息交換架構 SOAPSOAP 訊息基本屬於單向的資料傳遞,可以從用訊息基本屬於單向的資料傳遞,可以從用

戶端到伺服端使用戶端到伺服端使用 HTTPHTTP 送出請求訊息,在伺服送出請求訊息,在伺服器處理後將結果使用器處理後將結果使用 HTTPHTTP 回應到用戶端,如下回應到用戶端,如下圖所示:圖所示:

Page 16: 第 16 章  Web Service應用程式架構

HTTPHTTP 請求的請求的 SOAPSOAP 訊息訊息POST /BookDetail HTTP/1.1POST /BookDetail HTTP/1.1Host: www.company.comHost: www.company.comContent-Type: text/xml; charset="Big5"Content-Type: text/xml; charset="Big5"Content-Length: nnnnContent-Length: nnnnSOAPAction: " http://www.company.com/soap#getBookDetail"SOAPAction: " http://www.company.com/soap#getBookDetail"

<SOAP-ENV:Envelope <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoSOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">ding/">

<SOAP-ENV:Body><SOAP-ENV:Body> <m:getBookDetail xmlns:m="http://www.company.com/soap"><m:getBookDetail xmlns:m="http://www.company.com/soap"> <code>F8915</code><code>F8915</code> </m:getBookDetail></m:getBookDetail></SOAP-ENV:Body></SOAP-ENV:Body></SOAP-ENV:Envelope></SOAP-ENV:Envelope>

Page 17: 第 16 章  Web Service應用程式架構

HTTPHTTP 回應的回應的 SOAPSOAP 訊息訊息HTTP/1.1 200 OKHTTP/1.1 200 OKContent-Type: text/xml; charset="Big5"Content-Type: text/xml; charset="Big5"Content-Length: nnnnContent-Length: nnnn

<SOAP-ENV:Envelope <SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope"xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><SOAP-ENV:Body> <m:getBookDetailResponse xmlns:m="http://www.company.com/soap"><m:getBookDetailResponse xmlns:m="http://www.company.com/soap"> <book code="F8915"><book code="F8915"> <title>ASP<title>ASP 網頁製作徹底研究網頁製作徹底研究 </title></title> <author><author> 陳會安陳會安 </author></author> <price>580</price><price>580</price> </book></book> </m:getBookDetailResponse></m:getBookDetailResponse></SOAP-ENV:Body></SOAP-ENV:Body></SOAP-ENV:Envelope></SOAP-ENV:Envelope>

Page 18: 第 16 章  Web Service應用程式架構

16-3 HttpRequest16-3 HttpRequest物件建立物件建立 SOAPSOAP應用程式應用程式

SOAPSOAP 應用程式的架構應用程式的架構 HttpRequestHttpRequest 物件和物件和 SOAPSOAP

Page 19: 第 16 章  Web Service應用程式架構

SOAPSOAP 應用程式的架構應用程式的架構 SOAPSOAP 應用程式架構需要在用戶端送出應用程式架構需要在用戶端送出 HTTPHTTP 請請

求到伺服端,擁有用戶端和伺服端兩個應用程式,求到伺服端,擁有用戶端和伺服端兩個應用程式,其檔案如下表所示:其檔案如下表所示:應用程式檔名 說明

default.htm 用戶端應用程式,送出 SOAP請求訊息SOAPListener.asp 伺服端 SOAP傾聽者應用程式,接收請求訊息後,在查詢資料庫

後將取得的資料回應到用戶端SOAPTest.asp 這是 SOAP傾聽者應用程式的測試程式,用來在伺服端顯示測試

查詢結果的 XML文件Orders.mdb 第 13章的 Access資料庫,本節的範例使用 Books資料表的圖書資

Page 20: 第 16 章  Web Service應用程式架構

HttpRequestHttpRequest 物件和物件和 SOAPSOAP

MSXMLMSXML 剖析器的剖析器的 HttpRequestHttpRequest 物件可以物件可以直接在用戶端建立直接在用戶端建立 HTTPHTTP 請求和回應,其請求和回應,其架構如下圖所示:架構如下圖所示:

Page 21: 第 16 章  Web Service應用程式架構

16-4 WSDL16-4 WSDL 的基礎的基礎 SOAPSOAP 應用程式如何使用應用程式如何使用 WSDLWSDL 定義定義Web ServiceWeb Service 定義定義 Web ServiceWeb Service 傳送的訊息傳送的訊息 定義定義 RPCRPC 呼叫的輸入與輸出訊息呼叫的輸入與輸出訊息

Page 22: 第 16 章  Web Service應用程式架構

SOAPSOAP 應用程式如何使用應用程式如何使用 WSDL-1WSDL-1

「「WSDLWSDL」」 (Web Services Description (Web Services Description Language)Language) 屬於一種以屬於一種以 XMLXML 為基礎語言,為基礎語言,它是用來定義它是用來定義 Web ServiceWeb Service 和描述如何使和描述如何使用此用此 Web ServiceWeb Service 服務的語言。服務的語言。

Web ServiceWeb Service 是使用是使用 SOAPSOAP 作為資料傳遞作為資料傳遞的通訊協定,而的通訊協定,而 SOAPSOAP 應用程式可以透過應用程式可以透過WSDLWSDL 檔案的內容知道檔案的內容知道Web ServiceWeb Service 的位的位置、提供的置、提供的 RPCRPC 、訊息格式和如何使用這、訊息格式和如何使用這些函數呼叫。些函數呼叫。

Page 23: 第 16 章  Web Service應用程式架構

SOAPSOAP 應用程式如何使用應用程式如何使用 WSDL-2WSDL-2

用戶端首先取得用戶端首先取得 WSDLWSDL 檔案,可能儲存在本機或檔案,可能儲存在本機或是是Web ServiceWeb Service 所在的伺服器,在取得所在的伺服器,在取得 WSDLWSDL檔案後,就可以知道檔案後,就可以知道Web ServiceWeb Service 提供的服務提供的服務和如何使用它和如何使用它 (( 這就是這就是 SOAPSOAP 訊息的格式訊息的格式 )) ,現,現在我們就可以使用此在我們就可以使用此 Web SeviceWeb Sevice ,在傳送,在傳送SOAPSOAP 請求後,就可以等待請求後,就可以等待 SOAPSOAP 回應的結果。回應的結果。

Page 24: 第 16 章  Web Service應用程式架構

定義定義 Web ServiceWeb Service<definitions name="urn:GoogleSearch"<definitions name="urn:GoogleSearch" targetNamespace="urn:GoogleSearch"targetNamespace="urn:GoogleSearch" xmlns:typens="urn:GoogleSearch"xmlns:typens="urn:GoogleSearch" xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/"xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/"xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/">xmlns="http://schemas.xmlsoap.org/wsdl/"> ………… ………… <service name="GoogleSearchService"><service name="GoogleSearchService"> <port name="GoogleSearchPort" <port name="GoogleSearchPort" binding="typens:GoogleSearchBinding">binding="typens:GoogleSearchBinding"> <soap:address location="http://api.google.com/search/beta2"/><soap:address location="http://api.google.com/search/beta2"/> </port></port> </service></service></definitions></definitions>

Page 25: 第 16 章  Web Service應用程式架構

定義定義 Web ServiceWeb Service 傳送的訊息傳送的訊息<message name="doGoogleSearch"><message name="doGoogleSearch"> <part name="key" type="xsd:string"/><part name="key" type="xsd:string"/> <part name="q" type="xsd:string"/><part name="q" type="xsd:string"/> <part name="start" type="xsd:int"/><part name="start" type="xsd:int"/> <part name="maxResults" type="xsd:int"/><part name="maxResults" type="xsd:int"/> <part name="filter" type="xsd:boolean"/><part name="filter" type="xsd:boolean"/> <part name="restrict" type="xsd:string"/><part name="restrict" type="xsd:string"/> <part name="safeSearch" <part name="safeSearch"

type="xsd:boolean"/>type="xsd:boolean"/> <part name="lr" type="xsd:string"/><part name="lr" type="xsd:string"/> <part name="ie" type="xsd:string"/><part name="ie" type="xsd:string"/> <part name="oe" type="xsd:string"/><part name="oe" type="xsd:string"/></message></message>

Page 26: 第 16 章  Web Service應用程式架構

定義定義 Web ServiceWeb Service 傳送的訊息傳送的訊息<message name="doGoogleSearch"><message name="doGoogleSearch"> <part name="key" type="xsd:string"/><part name="key" type="xsd:string"/> <part name="q" type="xsd:string"/><part name="q" type="xsd:string"/> <part name="start" type="xsd:int"/><part name="start" type="xsd:int"/> <part name="maxResults" type="xsd:int"/><part name="maxResults" type="xsd:int"/> <part name="filter" type="xsd:boolean"/><part name="filter" type="xsd:boolean"/> <part name="restrict" type="xsd:string"/><part name="restrict" type="xsd:string"/> <part name="safeSearch" type="xsd:boolean"/><part name="safeSearch" type="xsd:boolean"/> <part name="lr" type="xsd:string"/><part name="lr" type="xsd:string"/> <part name="ie" type="xsd:string"/><part name="ie" type="xsd:string"/> <part name="oe" type="xsd:string"/><part name="oe" type="xsd:string"/></message></message>

Page 27: 第 16 章  Web Service應用程式架構

定義定義 RPCRPC 呼叫的輸入與輸出訊呼叫的輸入與輸出訊息息

在定義好在定義好 SOAPSOAP 請求和回應的訊息後,我們需要指定請求和回應的訊息後,我們需要指定RPCRPC 呼叫的輸入與輸出訊息,呼叫的輸入與輸出訊息, WSDLWSDL 使用使用 protTypeprotType 元元素定義每一個素定義每一個 RPCRPC 呼叫操作的輸入與輸出訊息,如下所呼叫操作的輸入與輸出訊息,如下所示:示:

<portType name="GoogleSearchPort"><portType name="GoogleSearchPort"> ……… ……… <operation name="doGoogleSearch"><operation name="doGoogleSearch"> <input message="typens:doGoogleSearch"/><input message="typens:doGoogleSearch"/> <output <output

message="typens:doGoogleSearchResponse"/>message="typens:doGoogleSearchResponse"/> </operation></operation></portType></portType>

Page 28: 第 16 章  Web Service應用程式架構

16-5 Google 16-5 Google 搜尋服務的 搜尋服務的 Web Web ServiceService

申請申請 GoogleGoogle 的搜尋服務的搜尋服務 使用使用 GoogleGoogle 搜尋服務的搜尋服務的 Web ServiceWeb Service

Page 29: 第 16 章  Web Service應用程式架構

申請申請 GoogleGoogle 的搜尋服務的搜尋服務 申請申請 GoogleGoogle 會員會員

• Google Google 會員是一種免費服務,請在 會員是一種免費服務,請在 Google Google 網站的新帳號網頁申請會員。網站的新帳號網頁申請會員。

取得取得 Web ServiceWeb Service 的授權碼的授權碼• 在申請好 在申請好 Google Google 會員且啟動帳號後,就可以會員且啟動帳號後,就可以申請 申請 Web Service Web Service 的授權碼。的授權碼。

取得取得 GoogleSearch.wsdlGoogleSearch.wsdl• ASP.NETASP.NET 程式範例是程式範例是 GoogleGoogle 搜尋服務的用戶搜尋服務的用戶

端程式,我們需要取得端程式,我們需要取得 GoogleSearch.wsdlGoogleSearch.wsdl的的WSDLWSDL 檔案和檔案和 C#C#類別榴,相關檔案是位在類別榴,相關檔案是位在Google Web APIGoogle Web API 。。

Page 30: 第 16 章  Web Service應用程式架構

使用使用 GoogleGoogle 搜尋服務的搜尋服務的 Web Web Service-1Service-1

取得取得 Web ServiceWeb Service的的 C#C#類別檔類別檔• 在在 googleapi.zipgoogleapi.zip 檔案就擁有檔案就擁有 C#C# 類別檔,請解開此類別檔,請解開此

檔案,在「檔案,在「 googleapi\dotnet\CSharp\Web googleapi\dotnet\CSharp\Web References\GoogleReferences\Google」資料夾下,就可以看到」資料夾下,就可以看到GoogleSearch.wsdlGoogleSearch.wsdl和和 Reference.csReference.cs 兩個檔案。兩個檔案。

在在 ASP.NETASP.NET 程式使用程式使用 Web ServiceWeb Service的的 C#C#類別類別檔案檔案• 將將 C#C# 類別檔案視為類別檔案視為 ASP.NETASP.NET 的外部類別檔案即可,的外部類別檔案即可,

如下所示:如下所示:<%@ Page language="vb" Src="Reference.cs"%><%@ Page language="vb" Src="Reference.cs"%>

Page 31: 第 16 章  Web Service應用程式架構

使用使用 GoogleGoogle 搜尋服務的搜尋服務的 Web Web Service-2Service-2

在在 ASP.NETASP.NET 程式以程式以 VB.NETVB.NET 語法來使用語法來使用 Web Web ServiceService ,首先匯入所需的名稱空間,如下所示:,首先匯入所需的名稱空間,如下所示:

<%@ Import <%@ Import Namespace="Google_Web_APIs_Demo.GoNamespace="Google_Web_APIs_Demo.Google" %>ogle" %>

在名稱空間後,在名稱空間後, GoogleGoogle的的Web ServiceWeb Service 就是就是建立建立 GoogleSearchServiceGoogleSearchService 物件,如下所示:物件,如下所示:

Dim objGoogle As GoogleSearchService = Dim objGoogle As GoogleSearchService = New GoogleSearchService()New GoogleSearchService()

Page 32: 第 16 章  Web Service應用程式架構

使用使用 GoogleGoogle 搜尋服務的搜尋服務的 Web Web Service-3Service-3

建立建立 GoogleSearchServiceGoogleSearchService 物件後,使物件後,使用用 doGoogleSearchdoGoogleSearch 方法取得方法取得 GoogleGoogle 的的搜尋結果的搜尋結果的 GoogleSearchResultGoogleSearchResult 物件,物件,如下所示:如下所示:

Dim objResult As GoogleSearchResult = Dim objResult As GoogleSearchResult = objGoogle.doGoogleSearch( _objGoogle.doGoogleSearch( _

GoogleKey, searchText, recordNo, PageSize, GoogleKey, searchText, recordNo, PageSize, False, "", False, "", "", "")False, "", False, "", "", "")

Page 33: 第 16 章  Web Service應用程式架構

使用使用 GoogleGoogle 搜尋服務的搜尋服務的 Web Web Service-4Service-4

傳回值是傳回值是 GoogleSearchResultGoogleSearchResult 物件,其物件,其相關屬性,如下表所示:相關屬性,如下表所示:

屬性 說明searchQuery 查詢的關鍵字字串searchTime 查詢花費的時間startIndex 查詢結果的開始筆數endIndex 查詢結果的結束筆數

estimatedTotalResultsCount 查詢結果的總筆數resultElements 取得查詢結果的 ResultElement 物件陣列

Page 34: 第 16 章  Web Service應用程式架構

使用使用 GoogleGoogle 搜尋服務的搜尋服務的 Web Web Service-5Service-5

resultElementsresultElements 屬性能夠取得查詢結果的網站屬性能夠取得查詢結果的網站資料,每一個網站是一個資料,每一個網站是一個 ResultElementResultElement 物件,物件,可以使用可以使用 ForFor迴路取得每一個物件,如下所示:迴路取得每一個物件,如下所示:

For i = 0 To For i = 0 To objResult.resultElements.GetUpperBound(0)objResult.resultElements.GetUpperBound(0)

objResult.resultElements(i).summary.ToString()objResult.resultElements(i).summary.ToString()

objResult.resultElements(i).URL.ToString()objResult.resultElements(i).URL.ToString()

………… …………

NextNext

Page 35: 第 16 章  Web Service應用程式架構

使用使用 GoogleGoogle 搜尋服務的搜尋服務的 Web Web Service-6Service-6

ResultElementResultElement 物件可以使用其屬性取得物件可以使用其屬性取得相關資料,如下表所示:相關資料,如下表所示:屬性 說明

summary 網站說明URL 網站的網址title 網站名稱

cachedSize 網頁尺寸directoryTitle 網站分類