Soap (Simple Object Access Protocal)
Soap (Simple Object Access Protocal) คือ โปรโตคอลที่อยู่ในรูปแบบของ xml ไว้แลกเปลี่ยนข้อมูลระหว่าง application
โปรโตคอล คือ มาตรฐานที่นำมากำหนดให้เป็นมาตรฐานและรูปแบบเดียวกัีน
โครงสร้าง Soap Message
|
Soap Envelop |
|
-soap header |
|
-soap body |
Soap header ไว้ระบุคำอธิบาย รายละเอียดความปลอดภัย ชื่อผู้ร้องขอเซอร์วิส สามารถระบุได้เองที่ตอน coding
ยกตัวอย่าง การเซตค่าให้กับ SOAP header
|
import org.apache.axis.client.Service; import org.apache.axis.client.Call; import org.apache.axis.encoding.XMLType; import org.apache.axis.message.SOAPHeaderElement; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode;
public class CalculateClient {
public static void main(String[] args)throws Exception{ String endpoint = "https://localhost:8443/axis/services/CalculatorWebservice?wsdl"; //wsdl String namespace = "http://services.arg.com"; //fix namespace for .net
System.setProperty("javax.net.ssl.trustStore","C:\\ssl-apache-tomcat-5.5.26\\mykeystore.jks"); String opr = "Add"; // operator name<method> Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(new java.net.URL(endpoint)); call.setOperationName( new QName(namespace, opr) ); call.addParameter( "a", XMLType.XSD_FLOAT, ParameterMode.IN); // parameter must match with .net call.addParameter( "b",XMLType.XSD_FLOAT,ParameterMode.IN); // type must match too call.setReturnType( XMLType.XSD_FLOAT); call.setUsername("wsuser"); call.setPassword("wspwd");
SOAPHeaderElement header = new SOAPHeaderElement("","","This is web service with authentication");
header.setActor("tassanawat"); header.setName("description"); header.setMustUnderstand(true); call.addHeader(header); Object result = call.invoke( new Object[] {10F,20F } ); //send parameter System.out.println("Result = " + result ); } } |
Soap body ไว้ระบุข้อมูลที่ร้องขอ และ ตอบกลับจากเว็บเซอร์วิส
ตัวอย่าง SOAP Request และ SOAP Response
Request
|
<?xml version="1.0" encoding="utf-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Header> <description soapenv:actor="tassanawat" xsi:type="soapenc:string" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">This is web service with authentication</description> </soapenv:Header> <soapenv:Body> <ns1:Add soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://services.arg.com"> <a href="#id0"/> <b href="#id1"/> </ns1:Add> <multiRef id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="xsd:float" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">10.0</multiRef> <multiRef id="id1" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="xsd:float" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/">20.0</multiRef> </soapenv:Body> </soapenv:Envelope> |
Response
|
<?xml version="1.0" encoding="UTF-8"?> <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <soapenv:Body> <ns1:AddResponse soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns1="http://services.arg.com"> <AddReturn xsi:type="xsd:float">30.0</AddReturn> </ns1:AddResponse> </soapenv:Body> </soapenv:Envelope> |