使用SAAJ访问soap

2 篇文章 0 订阅
1 篇文章 0 订阅

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());
		}
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值