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

HACK#58 การโปรแกรม Google Web API ด้วย C# และ .NET

สร้าง Google Search Application แบบ GUI ด้วย C# และ .NET framework

ใน Google Web API Developer’s Kit [ดู “The Google Web APIs Developer’s Kit”] มีตัวอย่างของ C# Visual Studio .NET (http://msdn.microsoft.com/vstudio) ซึ่งเป็น project ทางด้าน GUI Google Search Application (ดูในโฟลเดอร์ dotnet/cSharp) ซึ่งส่วนที่น่าสนใจที่สุดน่าจะเป็นโค้ดในไฟล์ Form1.cs

การแฮ็กในหัวข้อนี้จะเป็นการเตรียมโค้ดเบื้องต้นสำหรับ Google Search Application ซึ่งจะคล้ายๆกับการทำงานใน Perl [Hack #50], Java [Hack #56] และ Python [Hack #57]

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

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

// googly.cs

// A Google Web API C# console application

// Usage: googly.exe

// Copyright (c) 2002, Chris Sells.

// No warranties extended. Use at your own risk.

using System;

class Googly {

static void Main(string[] args) {

// Your Google API developer's key

string googleKey = "insert key here";

// Take the query from the command-line

if( args.Length != 1 ) {

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

return;

}

string query = args[0];

// Create a Google SOAP client proxy, generated by:

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

GoogleSearchService googleSearch = new GoogleSearchService( );

// Query Google

GoogleSearchResult results = googleSearch.doGoogleSearch(googleKey, query, 0, 10, false, "", false, "", "latin1", "latin1");

// No results?

if( results.resultElements == null ) return;

// Loop through results

foreach( ResultElement result in results.resultElements ) {

Console.WriteLine( );

Console.WriteLine(result.title);

Console.WriteLine(result.URL);

Console.WriteLine(result.snippet);

Console.WriteLine( );

}

}

}

อย่าลืมใส่ Google Developer’s key [ดู “Using the key in the hack”] (เช่น 12BuCK13mY5h0E/34KNocK@ttH3DoOR) ของคุณลงไปแทนใน “insert key here”

//Your Google API developer’s key

string googleKey = “12BuCK13mY5h0E/34KNocK@ttH3DoOR”;

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

ก่อนที่จะคอมไพล์โค้ด C# ได้นั้น คุณจะต้องสร้าง 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 GoogleSearch.wsdl

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

C:\GOOGLY.NET\CS>wsdl.exe 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\CS\GoogleSearchService.cs'.

ผลลัพธ์รายการสุดท้าย ซึ่งก็คือไฟล์ GoogleSearchService.cs จะมีหน้าตาลักษณะนี้

//--------------------------------------------------------------------------

// <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>

//--------------------------------------------------------------------------

//

// This source code was auto-generated by wsdl, Version=1.0.3705.288.

//

using System.Diagnostics;

using System.Xml.Serialization;

using System;

using System.Web.Services.Protocols;

using System.ComponentModel;

using System.Web.Services;

...

public System.IAsyncResult BegindoGoogleSearch(string key,

string q, int start, int maxResults, bool filter, string restrict,

bool safeSearch, string lr, string ie, string oe,

System.AsyncCallback callback, object asyncState) {

return this.BeginInvoke("doGoogleSearch", new object[] {

key,

q,

start,

maxResults,

filter,

restrict,

safeSearch,

lr,

ie,

oe}, callback, asyncState);

}


สำหรับไฟล์ googly.cs

C:\GOOGLY.NET\CS>csc /out:googly.exe *.cs

Microsoft (R) Visual C# .NET Compiler version 7.00.9466

for Microsoft (R) .NET Framework version 1.0.3705

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

Running the Hack

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

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

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

ผลลัพธ์

% googly.exe "WSDL while you work"

Axis/Radio interop, actual and potential

http://www.intertwingly.net/stories/2002/02/08/

axisradioInteropActualAndPotential.html <b>...</b> But

<b>you</b> might find more exciting services here

<b>...</b> Instead, we should <b>work</b>

together and<br> continuously strive to <b>...</b>

<b>While</b> <b>WSDL</b> is certainly far from

perfect and has many <b>...</b>

...

Simplified <b>WSDL</b>

http://capescience.capeclear.com/articles/simplifiedWSDL/

<b>...</b> So how does it <b>work</b>?

<b>...</b> If <b>you</b> would like to edit

<b>WSDL</b> <b>while</b> still avoiding<br> all

those XML tags, check out the <b>WSDL</b> Editor in

CapeStudio. <b>...</b>

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