ข้อดีของเว็บเซอร์วิสคือสามารถทำให้โปรแกรมต่างภาษา ต่างแพลตฟอร์ม ติดต่อทำงานร่วมกันได้ เว็บเซอร์วิสหนึ่งที่พัฒนาโดยคนไทยคือเว็บเซอร์วิสของ ปตท ซึ่งอยู่ที่ http://www.pttplc.com/pttinfo.asmx โดยที่มีเอกสารที่อธิบายเว็บเซอร์วิส (WSDL) อยู่ที่ http://www.pttplc.com/pttinfo.asmx?WSDL
ี่เว็บเซอร์วิสของ ปตท พัฒนาโดยใช้ Microsoft .NET แต่ เราสามารถที่จะเขียนโปรแกรมทั้งในภาษา Java และ ภาษา PHP เพื่อดูราคาน้ำมันในปัจจุบันได้
// file call_pttws1.php
<? include("../nusoap/nusoap.php"); $wsdl = "http://www.pttplc.com/pttinfo.asmx?WSDL"; //initial soap action $soapaction = "http://www.pttplc.com/ptt_webservice/CurrentOilPrice"; $lang ='EN'; //$proxyhost = "172.16.31.13"; //$proxyport = "80"; $client = new soapclient($wsdl,true); //, $proxyhost, $proxyport); $document = ''; $document .= ' '.$lang.''; $document .=''; //create soap message $mysoapmsg = $client->serializeEnvelope($document,'',array(),'document', 'literal'); //send soap message to soap server $response = $client->send($mysoapmsg, $soapaction); print_r($response); ?>
php_pttws
// file CurrentOilPrice รันโดยใช้ NetBeans 5.5 และ Library JAX-WS 2.0
package callsoapws; import javax.xml.transform.TransformerFactory; import javax.xml.transform.Transformer; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.Source; import javax.xml.soap.MessageFactory; import javax.xml.soap.SOAPConnection; import javax.xml.soap.SOAPConnectionFactory; import javax.xml.soap.SOAPMessage; import javax.xml.soap.SOAPBody; import javax.xml.soap.SOAPBodyElement; import javax.xml.soap.SOAPFactory; import javax.xml.soap.SOAPElement; import javax.xml.soap.MimeHeaders;
/** * @author Kanda Runapongsa * * TODO To change the template for this generated type comment go to Window - * Preferences - Java - Code Style - Code Templates */ public class CurrentOilPrice { public void msgEnvelope(String[] args) throws Exception { MessageFactory messageFactory = MessageFactory.newInstance();
// Create a message SOAPMessage message = messageFactory.createMessage();
// Get the SOAP header and body from the message // and remove the header //SOAPHeader header = message.getSOAPHeader(); SOAPBody body = message.getSOAPBody(); // header.detachNode();
// Create a SOAP factory SOAPFactory soapFactory = SOAPFactory.newInstance(); SOAPBodyElement operationElem = body.addBodyElement(soapFactory .createName("CurrentOilPrice", "ns", "http://www.pttplc.com/ptt_webservice/"));
SOAPElement language = operationElem.addChildElement(soapFactory .createName("Language", "ns", "http://www.pttplc.com/ptt_webservice/")); language.addTextNode("EN");
MimeHeaders hd = message.getMimeHeaders(); hd.addHeader("SOAPAction", "http://www.pttplc.com/ptt_webservice/CurrentOilPrice");
message.saveChanges(); System.out.println("REQUEST:"); //Display Request Message displayMessage(message);
System.out.println("\n\n");
SOAPConnection conn = SOAPConnectionFactory.newInstance() .createConnection(); SOAPMessage response = conn.call(message, "http://www.pttplc.com/pttinfo.asmx");
System.out.println("RESPONSE:"); //Display Response Message displayMessage(response); }
public void displayMessage(SOAPMessage message) throws Exception { TransformerFactory tFact = TransformerFactory.newInstance(); Transformer transformer = tFact.newTransformer(); Source src = message.getSOAPPart().getContent(); StreamResult result = new StreamResult(System.out); transformer.transform(src, result); }
public static void main(String[] args) throws Exception { CurrentOilPrice clientApp = new CurrentOilPrice(); clientApp.msgEnvelope(args); } }
Comments
www.tamdoo.com >>> เว็บมัน อับเดท แบบดุเดือดโดนใจ .......................................... ขอบคุณครับ เดี๋ยวเอาไปลองใช้ดู