java 发送soap 代码

package soap;

 

import java.io.FileInputStream;

import java.io.IOException;

import java.net.URL;

 

import javax.xml.soap.MessageFactory;

import javax.xml.soap.SOAPBody;

import javax.xml.soap.SOAPConnection;

import javax.xml.soap.SOAPConnectionFactory;

import javax.xml.soap.SOAPEnvelope;

import javax.xml.soap.SOAPException;

import javax.xml.soap.SOAPMessage;

import javax.xml.soap.SOAPPart;

import javax.xml.transform.Source;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerConfigurationException;

import javax.xml.transform.TransformerException;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.stream.StreamResult;

import javax.xml.transform.stream.StreamSource;

 

public class SOAPProcessor2 {

 

public static String invokeSrv(String endpoint) throws IOException {

try {

SOAPConnectionFactory connectionFactory = SOAPConnectionFactory.newInstance();

SOAPConnection connection = connectionFactory.createConnection();

URL client = new URL("http://localhost:8088/soapTest");

MessageFactory messageFactory = MessageFactory.newInstance();

 

SOAPMessage message = messageFactory.createMessage();

SOAPPart soapPart = message.getSOAPPart();

SOAPEnvelope envelope = soapPart.getEnvelope();

SOAPBody body = envelope.getBody();

//Populate the Message

       StreamSource preppedMsgSrc = new StreamSource(new FileInputStream("soapRequest.xml"));

       soapPart.setContent(preppedMsgSrc);

        //Save the message

message.saveChanges();

       //Set the destination

SOAPMessage response = connection.call(message, client);

 

 

//Create the transformer

       TransformerFactory transformerFactory = TransformerFactory.newInstance();

       Transformer transformer =  transformerFactory.newTransformer();

       //Extract the content of the reply

       Source sourceContent = response.getSOAPPart().getContent();

       //Set the output for the transformation

       StreamResult result = new StreamResult(System.out);

       transformer.transform(sourceContent, result);

       //System.out.println(result);

        //Close the connection            

       connection.close();

        

        

} catch (UnsupportedOperationException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (SOAPException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (TransformerConfigurationException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (TransformerException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

return "";

}

 

/**

* @param args

*/

public static void main(String[] args) {

try {

invokeSrv("");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

 

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值