java连接siebel的webservice中的接口,包括请求头验证token

import java.net.URL;
import javax.xml.soap.MessageFactory;
import javax.xml.soap.MimeHeader;
import javax.xml.soap.MimeHeaders;
import javax.xml.soap.SOAPBody;
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
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;

import org.w3c.dom.Node;

/**

  • 功能描述:模拟客户端发生soap报文调用服务器,服务器返回soap报文
  • @author tansy

*/
public class SysClient {

public static void main(String args[]) {  
    try {  
        // 创建连接  
        //实例化一个soap连接对象工厂  
        SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory  
                .newInstance();  
        //实例化一个连接对象
        SOAPConnection connection = soapConnFactory.createConnection();  
        //实例化一个消息对象
        MessageFactory messageFactory = MessageFactory.newInstance();  
        //实例化一个消息
        SOAPMessage message = messageFactory.createMessage();
       
        MimeHeaders mimeHeader=message.getMimeHeaders();
        //mimeHeader.setHeader("Content-Type", "text/xml;charset=UTF-8");
        //一开始这里没有定义soapaction,报了找不到web...错误,xxx代表需要调用的方法名
        mimeHeader.setHeader("SOAPAction", "\"document/http://siebel.com/CustomUI:xxxxxxxxx\"");
        //获取消息中soap消息部分的句柄
        SOAPPart soapPart = message.getSOAPPart();
        
        //获取soap消息部分中的信封句柄
        SOAPEnvelope envelope = soapPart.getEnvelope();
        envelope.setPrefix("soapenv");
        envelope.removeNamespaceDeclaration("SOAP-ENV");
   
        //添加namespace
        envelope.addNamespaceDeclaration("cus", "http://siebel.com/CustomUI");
        envelope.addNamespaceDeclaration("web", "http://siebel.com/webservices");
   
       
        //创建请求头
        SOAPHeader header = envelope.getHeader();
       //添加验证,为了token
		if (header == null)
		header = envelope.addHeader();
		header.setPrefix("soapenv");
        header.removeNamespaceDeclaration("SOAP-ENV");
		header.addChildElement("SessionToken", "web").addTextNode("");
		header.addChildElement("SessionType", "web").addTextNode("None");
		//密码
		header.addChildElement("PasswordText", "web").addTextNode("a");
		//用户名
		header.addChildElement("UsernameToken", "web").addTextNode("b");
		
   		//请求体
		SOAPBody body = envelope.getBody();  
		body.setPrefix("soapenv");
		body.removeNamespaceDeclaration("SOAP-ENV");
		SOAPElement bodyElement=body.addChildElement("xxxxxx", "cus");
		bodyElement.addChildElement("AllFlag", "cus").addTextNode("Y");
        // Save the message  
        message.saveChanges();  
        // 打印客户端发出的soap报文,做验证测试  
        System.out.println(" REQUEST: ");  
        message.writeTo(System.out);  
        System.out.println(" ");  
        /* 
         * 实际的消息是使用 call()方法发送的,该方法接收消息本身和目的地作为参数,并返回第二个 SOAPMessage 作为响应。 
         * call方法的message对象为发送的soap报文,url为mule配置的inbound端口地址。 
         */  
        URL url = new URL("http://xxxx:10082/eai_chs/start.swe?SWEExtSource=WebService&SWEExtCmd=Execute&WSSOAP=1");  
        System.out.println("URL----"+url);  
        // 响应消息  
        SOAPMessage reply = connection.call(message, url);  
        //reply.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "utf-8");  
        // 打印服务端返回的soap报文供测试  
        System.out.println("RESPONSE:");  
        // 创建soap消息转换对象  
        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);  
        // Close the connection
        System.out.println(""); 
        //返回接收到的内容
        SOAPPart response=reply.getSOAPPart();
       // log.info("返回的内容--------"+response.getEnvelope().getBody().getTextContent());
        connection.close();  
        /* 
         * 模拟客户端A,异常处理测试 
         */  
        SOAPBody ycBody = reply.getSOAPBody();  
        Node ycResp = ycBody.getFirstChild();  
        System.out.println("returnValue:"+ycResp.getTextContent());  
    } catch (Exception e) {  
        e.printStackTrace();  
        System.out.println(e.getMessage());  
    }  
}  

}

注意:如果出现下列错误信息,一定要添加soapaction

<?xml version="1.0" encoding="UTF-8"?>

SOAP-ENV:BodySOAP-ENV:FaultSOAP-ENV:Server不存在包含 ‘http://siebel.com/CustomUI:xxxxx’ 操作的有效 Web 服务。(SBL-EAI-04313)<siebelf:siebdetail xmlns:siebelf=“http://www.siebel.com/ws/fault”>siebelf:logfilenameEAIObjMgr_chs_0283_296747489.log</siebelf:logfilename>siebelf:errorstacksiebelf:errorsiebelf:errorcodeSBL-EAI-04313</siebelf:errorcode>siebelf:errorsymbolIDS_EAI_WS_OP_NOT_FOUND</siebelf:errorsymbol>siebelf:errormsg不存在包含 ‘http://siebel.com/CustomUI:xxxxx’ 操作的有效 Web 服务。(SBL-EAI-04313)</siebelf:errormsg></siebelf:error></siebelf:errorstack></siebelf:siebdetail></SOAP-ENV:Fault></SOAP-ENV:Body></SOAP-ENV:Envelope>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值