บทความนี้จะแสดงตัวอย่างการสร้างโปรแกรมโปรแกรม
client เพื่อทำการเรียกใช้ Web Service ที่ชื่อ BabelFish จากเว็บ http://www.xmethods.net/
โดยใช้ Delphi 6 มีขั้นตอนดังนี้
1. ให้ทำการเปิด Browser ไปยังเว็บ
http://www.xmethods.net/
แล้วคลิกที่ หัวข้อ BabelFish ดังรูป
![](./article/webservice1.gif)
2. ทำการคัดลอก ค่าของ WSDL URL เอาไว้
(WSDL URL มีค่าเป็น http://www.xmethods.net/sd/2001/BabelFishService.wsdl)
ดูรูปประกอบด้านล่าง
![](./article/webservice2.gif)
3.เปิดโปรแกรม Delphi แล้วเลือกวาง
Control Edit,Button จาก Component Palette Standard และ Control HTTPRIO
จาก Component Palette Webservices ดังรูป
![](./article/webservice3.gif)
4.เลือกเมนู File -> New ->
Other แล้วเลือก Web Services Importer จาก WebServices ดังรูป
![](./article/webservice4.gif)
5.ทำการป้อนค่าของ WSDL URL ลงในช่อง
ดังรูป แล้วคลิกปุ่ม Generate
![](./article/webservice5.gif)
โปรแกรมจะทำการสร้าง Unit2 ที่ใช้สำหรับการเรียกใช้
Web Service ขึ้นมา
6. ทำการเพิ่ม Unit2 ภายใต้ uses ของ
Unit1 ดังรูป
![](./article/webservice6.gif)
7. กำหนด Property ของ Control HTTPRIO1
ดังนี้
WSDLLocation =http://www.xmethods.net/sd/2001/BabelFishService.wsdl
Service =BabelFishService
Port =BabelFishPort
ดูรูปประกอบ
![](./article/webservice7.gif)
8. ทำการดับเบิ้ลคลิ้กที่ปุ่ม Button1
แล้วคัดลอกโค้ดต่อไปนี้ลงไป
procedure TForm1.Button1Click(Sender:
TObject);
var BabelService: BabelFishPortType;
begin
BabelService :=HTTPRIO1 as BabelFishPortType;
Memo2.Text :=BabelService.BabelFish('en_fr',Memo1.Text);
end; |
9. กดปุ่ม F9 เพื่อ Run โปรแกรม แล้วใส่ข้อความ
"Hello Word" บนช่องด้านบน ทำการกดปุ่ม Button1 รอสักครู่ โปรแกรมก็จะทำการเรียกใช้
BabelFish Web Service ที่มี translationmode
เป็นค่า 'en_fr' ในการแปลข้อความจาก English -> French ดังแสดงผลในรูปด้านล่างนี้
![](./article/webservice8.gif)
คุณสามารถที่จะกำหนดค่า translationmode
เพื่อแปลภาษาต่างๆได้ดังนี้
Translation |
translationmode |
English
-> French
English -> German
English -> Italian
English -> Portugese
English -> Spanish
French -> English
German -> English
Italian -> English
Portugese -> English
Russian -> English
Spanish -> English
|
'en_fr'
'en_de'
'en_it'
'en_pt'
'en_es'
'fr_en'
'de_en'
'it_en'
'pt_en'
'ru_en'
'es_en' |
?
|