1. 程式人生 > >soap訊息分析和soap訊息的傳遞和處理(一)

soap訊息分析和soap訊息的傳遞和處理(一)

WebService傳遞的時候實際上是傳遞一個SOAPMessage,我們來探究一下SOAPMessage的組成。

SOAPMessage由一個個的SOAP塊組成,這些SOAPPart被封裝到一個SOAPEnvelope(信封)中,信封中包括head和body。我們可以自己建立一個SOAPMessage傳送到服務提供端,從而返回WebService呼叫結果。

WebService服務建立程式碼參考上篇,本篇探討SOAPMessage的組成和傳送。

TestClient.java

package com.smile.service;

import java.io.IOException;
import java.net.URL;

import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPBodyElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPException;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;

import org.junit.Test;

public class TestClient {
	private String wsdlUrl = "http://localhost:8889/ns?wsdl";
	private String ns = "http://service.smile.com/";
	@Test
	public void test(){
		try {
			//建立訊息工廠
			MessageFactory factory = MessageFactory.newInstance();
			//根據訊息工廠建立SOAPMessage
			SOAPMessage message = factory.createMessage();
			//建立SOAPPart
			SOAPPart part = message.getSOAPPart();
			//獲取SOAPEnvelope soap信封
			SOAPEnvelope envelope = part.getEnvelope();
			//可以通過SOAPEnvelope獲取head body等資訊
			SOAPBody body = envelope.getBody();
			//根據QName 建立相應的節點
			QName qname = new QName("http://smile.web.com/webservice", "add", "ws");
			//<ws:add xmlns="http://smile.web.com/webservice"/>
			//使用以下方式設定< > 會被轉義
//			body.addBodyElement(qname).setValue("<a>1</a><b>2</b>");
			SOAPBodyElement element = body.addBodyElement(qname);
			element.addChildElement("a").setValue("1");
			element.addChildElement("b").setValue("2");
			try {
				message.writeTo(System.out);
			} catch (IOException e) {
				e.printStackTrace();
			}
		} catch (SOAPException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void test2(){
		try {
			//建立服務(Service)
			URL url = new URL(wsdlUrl);
			QName sname = new QName(ns,"MyServiceImplService"); 
			Service service = Service.create(url, sname);
			//建立Dispatch
			Dispatch<SOAPMessage> dispatch = service.createDispatch(new QName(ns,"MyServiceImplPort"),
				SOAPMessage.class,Service.Mode.MESSAGE);
			//建立訊息工廠
			MessageFactory factory = MessageFactory.newInstance();
			//根據訊息工廠建立SOAPMessage
			SOAPMessage message = factory.createMessage();
			//建立SOAPPart
			SOAPPart part = message.getSOAPPart();
			//獲取SOAPEnvelope soap信封
			SOAPEnvelope envelope = part.getEnvelope();
			//可以通過SOAPEnvelope獲取head body等資訊
			SOAPBody body = envelope.getBody();
			
			//根據QName 建立相應的節點
			QName ename = new QName(ns, "add", "ws");
			//<ws:add xmlns="http://smile.web.com/webservice"/>
			//使用以下方式設定< > 會被轉義
//			body.addBodyElement(qname).setValue("<a>1</a><b>2</b>");
			SOAPBodyElement element = body.addBodyElement(ename);
			element.addChildElement("a").setValue("22");
			element.addChildElement("b").setValue("33");
			message.writeTo(System.out);
			System.out.println("\t");
			System.out.println("Invoking.....");
			SOAPMessage response = dispatch.invoke(message);
			response.writeTo(System.out);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}