webservice xml java_java-Webservice-Client:使用纯文本xml而不是对...

可以使用SAAJ(带有Java附件API的SOAP)运行,其运行级别低于JAX-WS.我希望它比JAX-WS使用更少的系统资源.

No more dynamic construction of the SOAP message this time, let’s use a simple text editor and type the soap message we want to send.

Example 1-13. The formated SOAP message in a text file: prepared.msg

SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"

xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"

xmlns:enc="http://schemas.xmlsoap.org/soap/encoding/"

xmlns:xsd="http://www.w3.org/2001/XMLSchema"

xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">

your message or e-mail

Now the code will be shorter and you can easily use it for testing purpose.

Example 1-14. Post a SOAP message in a text file, to a web service using SAAJ

import javax.xml.soap.SOAPConnectionFactory;

import javax.xml.soap.SOAPConnection;

import javax.xml.soap.MessageFactory;

import javax.xml.soap.SOAPMessage;

import javax.xml.soap.SOAPPart;

import java.io.FileInputStream;

import javax.xml.transform.stream.StreamSource;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.Transformer;

import javax.xml.transform.Source;

import javax.xml.transform.stream.StreamResult;

public class Client {

public static void main(String[] args) {

try {

// Create the connection

SOAPConnectionFactory scf = SOAPConnectionFactory.newInstance();

SOAPConnection conn = scf.createConnection();

// Create message

MessageFactory mf = MessageFactory.newInstance();

SOAPMessage msg = mf.createMessage();

// Object for message parts

SOAPPart sp = msg.getSOAPPart();

StreamSource prepMsg = new StreamSource(

new FileInputStream("path/prepared.msg"));

sp.setContent(prepMsg);

// Save message

msg.saveChanges();

// View input

System.out.println("

Soap request:

");

msg.writeTo(System.out);

System.out.println();

// Send

String urlval = "http://www.pascalbotte.be/rcx-ws/rcx";

SOAPMessage rp = conn.call(msg, urlval);

// View the output

System.out.println("

XML response

");

// Create transformer

TransformerFactory tff = TransformerFactory.newInstance();

Transformer tf = tff.newTransformer();

// Get reply content

Source sc = rp.getSOAPPart().getContent();

// Set output transformation

StreamResult result = new StreamResult(System.out);

tf.transform(sc, result);

System.out.println();

// Close connection

conn.close();

}

catch (Exception e) {

System.out.println(e.getMessage());

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值