วันพุธที่ 8 สิงหาคม พ.ศ. 2550

HACK#59 การเขียนโปรแกรม Google Web API ด้วย VB.NET

สร้าง Google Search Application แบบ GUI ด้วย Visual Basic และ .NET Framework

นอกจากที่มีตัวอย่างในการพัฒนา Google Search Application ด้วย C# [Hack #58] แล้ว Google Web APIs Developer’s Kit ได้มีตัวอย่างของ Google Search ใน Visual Basic รวมเอาไว้อยู่ด้วยเช่นกัน ในขณะที่คุณอาจจะผสมผเสรวบรวมสิ่งต่างๆที่จำเป็นเกือบจะทั้งหมดจาก Google Demo Form.vb ซึ่งเป็นโค้ดที่มีมาให้อยู่ในชุด kit ดังกล่าวอยู่แล้ว การแฮ็กในหัวข้อนี้ก็ได้เตรียมโค้ดพื้นฐานอันเป็นเบื้องต้นสำหรับ Google Search Application โดยปราศจากข้อกังขาในการพัฒนาถึงขั้นความสามารถอันเต็มพิกัดของโปรเจ็ค Visual Studio .NET

  • Tip: การคอมไพล์และรันการแฮ็กนี้จำเป็นต้องมี .NET Framework (http://msdn.microsoft.com/library/default.asp?url=/nhp/default.asp?contentid=28000519) ติดตั้งอยู่ด้วย

โค้ดตัวอย่าง

' googly.vb

' A Google Web API VB.NET console application

' Usage: googly.exe

' Copyright (c) 2002, Chris Sells.

' No warranties extended. Use at your own risk.

Imports System

Module Googly

Sub Main(ByVal args As String( ))

' Your Google API developer's key

Dim googleKey As String = "insert key here"

' Take the query from the command-line

If args.Length <> 1 Then

Console.WriteLine("Usage: google.exe ")

Return

End If

Dim query As String = args(0)

' Create a Google SOAP client proxy, generated by:

' c:\> wsdl.exe /l:vb http://api.google.com/GoogleSearch.wsdl

Dim googleSearch As GoogleSearchService = New GoogleSearchService( )

' Query Google

Dim results As GoogleSearchResult = googleSearch.doGoogleSearch(googleKey, query, 0, 10, False, "", False, "", "latin1", "latin1")

' No results?

If results.resultElements Is Nothing Then Return

' Loop through results

Dim result As ResultElement

For Each result In results.resultElements

Console.WriteLine( )

Console.WriteLine(result.title)

Console.WriteLine(result.URL)

Console.WriteLine(result.snippet)

Console.WriteLine( )

Next

End Sub

End Module

โปรดอย่าลืมใส่ Google Developer Key (เช่น 12BuCK13mY5h0E/34KNocK@ttH3DoOR)

ของคุณลงใน “insert key here”

‘Your Google API developer’s key

Dim googleKey As String = “12BuCK13mY5h0E/34KNocK@ttH3DoOR”

การคอมไพล์โค้ด

ก่อนที่จะคอมไพล์โค้ด VB Application ได้นั้น คุณจะต้องสร้าง Google SOAP Client Proxy เสียก่อน ซึ่ง Google SOAP Client Proxy นี้จะเป็นโค้ดจำนวนหนึ่งที่สร้างตามรายละเอียด (specification) ในไฟล์ GoogleSearch.wsdl ซึ่งจะประกอบด้วย XML based description เกี่ยวกับ Google Web Service, mothod, parameter, และ return value แต่นับว่าโชคดีที่ .NET Framework kit ที่มีมาให้นั้น มีไฟล์ wsdl.exe ซึ่งทำให้คุณไม่ต้องมาเขียนโค้ดด้วยตัวคุณเอง

  • Tip: นี่อาจเป็นสิ่งที่แปลกสักหน่อยหากคุณได้ลองนึกดู เนื่องจากส่วนสำคัญในการเชื่อมต่อกับ Web Service กลับถูกสร้างขึ้นจาก description ของมันเองโดยอัตโนมัติ

เรียกไฟล์ wsdl.exe จากไดเรกทอรีที่ GoogleSearch.wsdl อยู่ ดังนี้

C:\GOOGLY.NET>wsdl.exe /l:vb GoogleSearch.wsdl

คุณอาจจะระบุถึงไฟล์ wsdl.exe ที่อยู่ในเว็บไซต์ของ Google ก็ได้เช่นกัน

C:\GOOGLY.NET\VB>wsdl.exe /l:vb http://api.google.com/GoogleSearch.wsdl

Microsoft (R) Web Services Description Language Utility

[Microsoft (R) .NET Framework, Version 1.0.3705.0]

Copyright (C) Microsoft Corporation 1998-2001. All rights reserved.

Writing file 'C:\GOOGLY.NET\VB\GoogleSearchService.vb'.

ผลลัพธ์ที่คุณจะได้รับไฟล์ GoogleSearchService.vb นั้นจะมีหน้าตาลักษณะนี้

'---------------------------------------------------------------------------

' <autogenerated>

' This code was generated by a tool.

' Runtime Version: 1.0.3705.288

'

' Changes to this file may cause incorrect behavior and will be lost if

' the code is regenerated.

' </autogenerated>

'---------------------------------------------------------------------------

Option Strict Off

Option Explicit On

Imports System

Imports System.ComponentModel

Imports System.Diagnostics

Imports System.Web.Services

Imports System.Web.Services.Protocols

Imports System.Xml.Serialization

...

Public Function BegindoGoogleSearch(ByVal key As String, ByVal q As

String, ByVal start As Integer, ByVal maxResults As Integer, ByVal

filter As Boolean, ByVal restrict As String, ByVal safeSearch As

Boolean, ByVal lr As String, ByVal ie As String, ByVal oe As String,

ByVal callback As System.AsyncCallback, ByVal asyncState As Object) As

System.IAsyncResult

Return Me.BeginInvoke("doGoogleSearch", New Object( ) {key, q,

start, maxResults, filter, restrict, safeSearch, lr, ie, oe}, callback,

asyncState) End Function

'<remarks/>

Public Function EnddoGoogleSearch(ByVal asyncResult As System.IAsyncResult) As GoogleSearchResult

Dim results( ) As Object = Me.EndInvoke(asyncResult)

Return CType(results(0),GoogleSearchResult)

End Function

End Class

สำหรับการคอมไพล์ไฟล์ googly.vb

C:\GOOGLY.NET\VB>vbc /out:googly.exe *.vb

Microsoft (R) Visual Basic .NET Compiler version 7.00.9466

for Microsoft (R) .NET Framework version 1.00.3705

Copyright (C) Microsoft Corporation 1987-2001. All rights reserved.

Running the Hack

รัน Googly ที่ command line โดยการใส่คำถามที่ต้องการเข้าไปแทนที่ query word

C:\GOOGLY.NET\VB>googly.exe “query words”

  • Tip: หน้าต่างของ dos command อาจจะไม่กว้างพอสำหรับการแสดงผล ซึ่งคุณสามารถแก้ปัญหาได้โดยการส่งผลลัพธ์ที่ได้จากการค้นหาไปยังไฟล์อื่นใด เพื่อใช้เอดิเตอร์ (editor) ตรวจดูผลลัพธ์ได้ ซึ่งการส่งผลลัพธ์ไปยังไฟล์ดังกล่าวทำได้โดย เพิ่ม > results.txt ต่อท้ายเข้าไปที่คำสั่งข้างต้น

ผลลัพธ์

ขั้นตอนต่างๆของการแฮ็กในหัวข้อนี้จะเหมือนกับในส่วนของ C# [Hack #58] และตามปกติเมื่อสั่งรันแล้วก็ควรจะได้ผลลัพธ์เหมือนๆกันด้วยเช่นกัน

0 ความคิดเห็น: