使用VB.NET實現 Google Web Service

來源:互聯網
上載者:User
google|web web service 是當今網際網路世界中最重要的開發技術之一,我們可通過使用XML (可延伸標記語言 (XML))、 SOAP (簡易物件存取通訊協定 (SOAP))、WSDL (Web 服務描述語言)和UDDI (統一描述、發現和整合協議)以標準方式將web service技術用於業務應用和用戶端之間的串連。

XML可用於構造資料、SOAP 可用於資料轉送、WSDL 可用於描述服務而UDDI 可用於擷取可用服務的列表。Web service 使得應用程式無須考慮硬體系統、作業系統和程式設計語言就可以進行相互連信。

web service與以前的模型不同之處就在於它本身並不提供使用者介面,相反web service公開揭示了可程式化的商務邏輯。因此,使用者可根據需要將自己的介面添加到應用程式中。

本文中,我們將會學習如何使用 Microsoft Visual Basic.Net來實現Google web service。


Google Web Service


Google是一個很重要的web網站,它向公眾提供web服務,允許應用程式使用如搜尋和拼字檢查之類的功能。現在,我們來看一看如何通過Visual Basic.NET在應用程式中使用此服務。

在訪問Google web service之前,我們需要建立一個Google帳戶並獲得一個許可密鑰,只有這樣,我們才能在一天中進行1000 個左右的自動查詢。

建立Google 帳戶時請訪問http://www.Google.co.nz/apis/。一旦輸入了電子郵件地址和口令,Google 就會通過email 將您的許可密鑰發送到您的信箱中。我們會在本文的樣本中使用許可密鑰。

從這裡開始

現在我們已經獲得了所需的許可密鑰,接下來我們將在Visual Basic.NET中建立一個應用程式,以便通過使用Google的web service API(API)來建立自訂的搜尋和拼字檢查器。

請開啟 Visual Studio .NET ,然後建立一個新的Windows 應用程式工程。將此工程命名為googleapi 並單擊確定:





添加指向Google Web Service的Web引用

下一步,我們需要添加指向Google Web Service的Web引用(這與添加指向 COM/ActiveX 對象的引用非常相似,但是添加Web引用後,我們就有權訪問Google 伺服器上的XML web service)。

請開啟您的solution explorer,按右鍵references並單擊添加web reference,或者您可以選擇工程菜單然後單擊add web reference。

在地址欄中,請鍵入http://api.Google.com/GoogleSearch.wsdl (注意:請確保你所鍵入的內容正確無誤,即與所顯示的完全一樣,尤其要注意該 URL是區分大小寫):





在輸入URL地址並按下斷行符號鍵之後,Google web service就會匯入,您看到的螢幕應該與上面樣本中所顯示的視窗類別似。最後,單擊添加引用按鈕將此web 引用添加到我們工程中。

執行Google Web Service

請在解決方案瀏覽器視窗中單擊web 引用,這樣就可以查看我們在此之前已添加的Google web 引用 。我們將其重新命名為Google,具體方法是按右鍵此引用並單擊重新命名:





建立使用者介面,如下圖所示。添加下列控制項:

a) 用於搜尋:

txtSearch - 文字框

lbl_TotalFound - 標籤

btn_Search - 按鈕

b) 用於拼字檢查:

txt_CheckSpelling - 文字框

lbl_CorrectSpelling - 標籤

btn_CheckSpelling 按鈕





請將下列代碼輸入到Google 搜尋按鈕(btn_Search)的單擊事件中:

Private Sub btn_Search_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btn_Search.Click
Dim MyLicenseKey As String ' Variable to Store the License Key
' Declare variable for the Google search service
Dim MyService As Google.GoogleSearchService = New _
Google.GoogleSearchService()
' Declare variable for the Google Search Result
Dim MyResult As Google.GoogleSearchResult
' Please Type your license key here
MyLicenseKey = "tGCTJkYos3YItLYzI9Hg5quBRY8bGqiM"
' Execute Google search on the text enter and license key
MyResult = MyService.doGoogleSearch(MyLicenseKey, _
txtSearch.Text, 0, 1, False, "", False, "", "", "")
' output the total Results found
lbl_TotalFound.Text = "Total Found : " & _
CStr(MyResult.estimatedTotalResultsCount)
End Sub



請將下列代碼輸入到拼字檢查按鈕(btn_CheckSpelling)的單擊事件中:

Private Sub btn_CheckSpelling_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btn_CheckSpelling.Click
Dim MyLicenseKey As String ' Variable to Store the License Key
' Declare variable for the Google search service
Dim MyService As Google.GoogleSearchService = New _
Google.GoogleSearchService()
' Declare variable for the Google Search Result
Dim MyResult As String
' Please Type your license key here
MyLicenseKey = "tGCTJkYos3YItLYzI9Hg5quBRY8bGqiM"
' Execute Google search on the text enter and license key
MyResult = MyService.doSpellingSuggestion(MyLicenseKey, _
txt_CheckSpelling.Text)
' output the Results
lbl_CorrectSpelling.Text = "Did you Mean : " & MyResult
End Sub



現在我們已完成了應用程式的編碼工作,接下來就可以運行應用程式並在搜尋方塊中鍵入文本,然後單擊google 搜尋按鈕查看所找到的結果的數目。我們還可以對Google拼字檢查進行測試和驗證。





我們的web service 實現了預期的目標,可正常運轉。而我們的實現卻僅僅寥寥幾行代碼而已!


結論


本文詳細描述了如何將web service 整合到應用程式中。您可以對此訪問進行如下處理:

·發布定期訂閱的搜尋請求以監控web 有關某一主體的新資訊。

·進行市場調查,方法是分析不同主題可用資訊量的差別。

·通過非HTML 介面進行搜尋,如命令列、論文或者可視化應用程式。

·開展創新活動來充分使用 web 上的資訊。

·將 Google 拼字檢查添加到應用程式。

Google web service 支援的搜尋文法與 Google.com web 網站所支援的搜尋文法相同。同時它為註冊使用 Google web service 的開發使用者每天提供最多1,000 次查詢(這個數目對於小型/中型應用程式來說已經足夠使用了)



聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.