package test;
import org.w3c.dom.Document;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.net.URL;
import java.nio.charset.Charset;
import javax.xml.namespace.QName;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
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.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.soap.SOAPBinding;
/**
* QName 创建多个xmlns不成功
* 使用String编写SOAP Header
* @author glacierliu
*
*/
public class Ctest1 {
// private static String ns = "http://service.soap.org/";
// private static String wsdlUrl = "http://localhost:8989/ms?wsdl";
private static final QName SERVICE_NAME = new QName("http://example1/",
"MyServiceImpl");
private static final QName PORT_NAME = new QName("http://example1/",
"MyServiceImplPort");
private static final String OTA = "http://www.opentravel.org/OTA/2003/05";
private static final String SEEKDA = "http://connect.seekda.com/2009/04";
private static final String EndPointURL = "https://switch.seekda.com/switch/1.009/ChannelConnect";
public static void main(String[] args) {
Ctest1.test1();
}
public static void test1() {
try {
//1 创建服务(Service)
//URL url = new URL(wsdlUrl);
//QName sname = new QName(ns,"MyServiceImplService");
//Service service = Service.create(url,sname);
Service service = Service.create(SERVICE_NAME);
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING,
EndPointURL);
//2 创建Dispatch
Dispatch<SOAPMessage> dispatch = service.createDispatch(PORT_NAME,
SOAPMessage.class, Service.Mode.MESSAGE);
//3 创建SOAPMessage
SOAPMessage msg = formartSoapString(createMessage());
msg.writeTo(System.out);
System.out.println("\n invoking.....waiting response ......");
//4 通过Dispatch传递消息,会返回响应消息
SOAPMessage response = dispatch.invoke(msg);
response.writeTo(System.out);
System.out.println("\n----------------------------------------");
//5 将响应的消息转换为dom对象
Document doc = response.getSOAPPart().getEnvelope().getBody()
.extractContentAsDocument();
String str1 = doc.getElementsByTagName(
"ota:HotelDescriptiveContents").item(0).getTextContent();
System.out.println(str1);
String str2 = doc.getElementsByTagName("Description").item(0)
.getTextContent();
System.out.println(str2);
} catch (SOAPException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void test02() {
try {
//1 创建服务(Service)
//URL url = new URL(wsdlUrl);
//QName sname = new QName(ns,"MyServiceImplService");
//Service service = Service.create(url,sname);
Service service = Service.create(SERVICE_NAME);
service.addPort(PORT_NAME, SOAPBinding.SOAP11HTTP_BINDING,
EndPointURL);
//2 创建Dispatch
Dispatch<SOAPMessage> dispatch = service.createDispatch(PORT_NAME,
SOAPMessage.class, Service.Mode.MESSAGE);
//3 创建SOAPMessage
SOAPMessage msg = MessageFactory.newInstance().createMessage();
SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
SOAPBody body = envelope.getBody();
//4 创建QName来指定消息中传递数据
QName nameOta = new QName(OTA, "OTA_HotelDescriptiveInfoRQ", "ota"); //<nn:add xmlns="xx"/>
QName nameSeekda = new QName(OTA, "OTA_HotelDescriptiveInfoRQ",
"ota"); //<nn:add xmlns="xx"/>
SOAPBodyElement ele = body.addBodyElement(nameOta);
ele = body.addBodyElement(nameSeekda);
ele.addChildElement("a").setValue("22");
ele.addChildElement("b").setValue("33");
msg.writeTo(System.out);
System.out.println("\n invoking.....");
//5 通过Dispatch传递消息,会返回响应消息
SOAPMessage response = dispatch.invoke(msg);
response.writeTo(System.out);
System.out.println("\n----------------------------------------");
//将响应的消息转换为dom对象
Document doc = response.getSOAPPart().getEnvelope().getBody()
.extractContentAsDocument();
String str = doc.getElementsByTagName("addResult").item(0)
.getTextContent();
System.out.println(str);
} catch (SOAPException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* <pre>
* 把soap字符串格式化为SOAPMessage
* 功能详细描述
* </pre>
* @param message
* @return
* @see [类、类#方法、类#成员]
*/
public static SOAPMessage formartSoapString(String message) {
MessageFactory msgFactory;
try {
msgFactory = MessageFactory.newInstance();
SOAPMessage reqMsg = msgFactory.createMessage(new MimeHeaders(),
new ByteArrayInputStream(message.getBytes(Charset.forName(
"UTF-8"))));
reqMsg.saveChanges();
return reqMsg;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String createMessage() {
String soapHeader = "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Header/><SOAP-ENV:Body>";
String soapEnder = "</SOAP-ENV:Body></SOAP-ENV:Envelope>";
StringBuffer msg = new StringBuffer();
msg.append(soapHeader);
msg.append(
"<ota:OTA_HotelDescriptiveInfoRQ Version=\"6.003\" xmlns:ota=\"http://www.opentravel.org/OTA/2003/05\" xmlns:seekda=\"http://connect.seekda.com/2009/04\">");
msg.append("<ota:POS>");
msg.append("<ota:Source TerminalID=\"87.230.100.58\">");
msg.append(
"<ota:RequestorID URL=\"mailto:oliver.bollow@tropo.de\" ID=\"N/A\" Type=\"10\" MessagePassword=\"AA8YWWAR6BLW\"/>");
msg.append("<ota:BookingChannel Type=\"7\">");
msg.append("<ota:CompanyName Code=\"Integration\"/>");
msg.append("</ota:BookingChannel>");
msg.append("<ota:TPA_Extensions>");
msg.append("<seekda:Lock>");
msg.append("<seekda:LockRequest />");
msg.append("</seekda:Lock>");
msg.append("</ota:TPA_Extensions>");
msg.append("</ota:Source>");
msg.append("</ota:POS>");
msg.append("<ota:HotelDescriptiveInfos>");
msg.append("<ota:HotelDescriptiveInfo HotelCode=\"CHNL_TROPO\"/>");
msg.append("</ota:HotelDescriptiveInfos>");
msg.append("</ota:OTA_HotelDescriptiveInfoRQ>");
msg.append(soapEnder);
return msg.toString();
}
}