1.sopa1.1
package SAAJ.my;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
public class TestSAAJSoap1_1 {
public static void main(String args[]) {
try {
// First create the connection
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory
.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();
// Next, create the actual message
MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_1_PROTOCOL);
SOAPMessage message = messageFactory.createMessage();
//HTTP请求的表头信息
MimeHeaders headers =message.getMimeHeaders();
headers.addHeader("SOAPAction", "http://WebXml.com.cn/getWeatherbyCityName");
// Create objects for the message parts
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
//去掉soap消息的header部分
SOAPHeader header = envelope.getHeader();
header.detachNode();
SOAPBody body = envelope.getBody();
// Populate the body
// Create the main element and namespace
SOAPElement bodyElement = body.addChildElement(envelope.createName(
"getWeatherbyCityName", "", "http://WebXml.com.cn/"));
// Add content
bodyElement.addChildElement("theCityName").addTextNode("广州");
// Save the message
message.saveChanges();
// Check the input
System.out.println("\\nREQUEST:\\n");
message.writeTo(System.out);
System.out.println();
// Send the message and get a reply
// Set the destination
String destination = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
// Send the message
SOAPMessage reply = connection.call(message, destination);
//Check the output
System.out.println("\\nRESPONSE:\\n");
//Create the transformer
TransformerFactory transformerFactory =
TransformerFactory.newInstance();
Transformer transformer =
transformerFactory.newTransformer();
//Extract the content of the reply
Source sourceContent = reply.getSOAPPart().getContent();
//Set the output for the transformation
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
System.out.println();
connection.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
2.soap1.2
package SAAJ.my;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPConstants;
import javax.xml.soap.SOAPElement;
import javax.xml.soap.SOAPEnvelope;
import javax.xml.soap.SOAPHeader;
import javax.xml.soap.SOAPMessage;
import javax.xml.soap.SOAPPart;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
public class TestSAAJSoap1_2 {
public static void main(String args[]) {
try {
// First create the connection
SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory
.newInstance();
SOAPConnection connection = soapConnFactory.createConnection();
// Next, create the actual message
MessageFactory messageFactory = MessageFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
SOAPMessage message = messageFactory.createMessage();
// Create objects for the message parts
SOAPPart soapPart = message.getSOAPPart();
SOAPEnvelope envelope = soapPart.getEnvelope();
//去掉soap消息的header部分
SOAPHeader header = envelope.getHeader();
header.detachNode();
SOAPBody body = envelope.getBody();
// Populate the body
// Create the main element and namespace
SOAPElement bodyElement = body.addChildElement(envelope.createName(
"getWeatherbyCityName", "", "http://WebXml.com.cn/"));
// Add content
SOAPElement soapElement = bodyElement.addChildElement("theCityName");
soapElement.addTextNode("上海");
// Save the message
message.saveChanges();
// Check the input
System.out.println("\\nREQUEST:\\n");
message.writeTo(System.out);
System.out.println();
// Send the message and get a reply
// Set the destination
String destination = "http://www.webxml.com.cn/WebServices/WeatherWebService.asmx";
// Send the message
SOAPMessage reply = connection.call(message, destination);
//Check the output
System.out.println("\\nRESPONSE:\\n");
//Create the transformer
TransformerFactory transformerFactory =
TransformerFactory.newInstance();
Transformer transformer =
transformerFactory.newTransformer();
//Extract the content of the reply
Source sourceContent = reply.getSOAPPart().getContent();
//Set the output for the transformation
StreamResult result = new StreamResult(System.out);
transformer.transform(sourceContent, result);
System.out.println();
connection.close();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}