当前位置:编程学习 > JAVA >>

axis2_1.4 jdk1.4 访问wenservice的时候发现soap消息全部乱码

1.开始客户只给了我们一个https://......的链接,可以拿到wsdl

2.我用axis2直接生成客户端带啊,使用jdk1.6,结果运行正常

3.当我发现我们系统必须使用jdk1.4的时候,悲剧发生了,axis2_1.6和jdk1.4不兼容,于是换成axis2_1.4

4.然后我用tcpMon来观察,发现soap消息全部都是乱码,所有的都是乱码,不是中文乱码

5.后来我抛弃axis2 改成直接使用soap API来 发送消息,发现问题依然存在,代码如下,希望大侠们多多关照,分数不多,好人一生平安


      
      
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.xml.soap.MessageFactory;
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;


      
    /** 
     * 功能描述:模拟客户端A-即服务调用者,通过该类模拟客户端发送soap报文给mule, 
     * 同时把mule的响应报文打印出来做测试 
     * @author liuxp 
     * 
     */  
    public class SynClient {  
      
        public static void main(String args[]) {  
      
            try {  
      
                // 创建连接  
                // ==================================================  
                SOAPConnectionFactory soapConnFactory = SOAPConnectionFactory.newInstance();  
                SOAPConnection connection = soapConnFactory.createConnection();  
      
                //  创建消息对象  
                // ===========================================  
                MessageFactory messageFactory = MessageFactory.newInstance();  
                SOAPMessage message = messageFactory.createMessage();
    //          message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "gb2312");  
      
                // 创建soap消息主体==========================================  
                SOAPPart soapPart = message.getSOAPPart();// 创建soap部分
                SOAPEnvelope envelope = soapPart.getEnvelope();
                envelope.addNamespaceDeclaration("wsse", "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd");
                envelope.addNamespaceDeclaration("xsi", "http://www.w3.org/2001/XMLSchema-instance");
                envelope.addNamespaceDeclaration("ser", "http://service.svsxml.svs.com");
                SOAPHeader header = envelope.getHeader();
                SOAPElement secEle = header.addChildElement("Security","wsse");
                secEle.addAttribute(envelope.createName("mustUnderstant"), "1");
                SOAPElement tokenEle = secEle.addChildElement("UsernameToken","wsse");
                tokenEle.addChildElement("Username","wsse").addTextNode("extsincere");
                tokenEle.addChildElement("Password","wsse").addTextNode("Si903Re13");
                
                /**
                 <soapenv:Header>
<wsse:Security wsse:mustUnderstant="1">
<wsse:UsernameToken>
<wsse:Username>*****</wsse:Username>
<wsse:Password>*****</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
                 */
                SOAPBody body = envelope.getBody();
                SOAPElement cardActivationEle = body.addChildElement("cardActivation", "ser");
                SOAPElement requestEle = cardActivationEle.addChildElement("request");
                
                //添加activationAmount Element
                SOAPElement amountEle = requestEle.addChildElement("activationAmount");
                amountEle.addChildElement("amount").addTextNode("100.0");
                amountEle.addChildElement("currency").addTextNode("HKD");
                
                //添加card Element
                SOAPElement cardEle = requestEle.addChildElement("card");
                cardEle.addChildElement("cardNumber").addTextNode("5045075939749901756");
                cardEle.addChildElement("pinNumber");
                cardEle.addChildElement("cardTrackOne");
                cardEle.addChildElement("cardTrackTwo");
                
                //添加 date Element
                requestEle.addChildElement("date").addTextNode("2013-09-12T15:26:46");
                
                //添加 Invoice number Element
                requestEle.addChildElement("invoiceNumber").addTextNode("00000001");
                
                //添加 merchant Element
                SOAPElement merchantEle = requestEle.addChildElement("merchant");
                merchantEle.addChildElement("merchantName").addTextNode("Sincere Hong Kong");
                merchantEle.addChildElement("merchantNumber").addTextNode("508307");
                merchantEle.addChildElement("storeNumber").addTextNode("001");
                merchantEle.addChildElement("division");
                
                //添加 routingID
                requestEle.addChildElement("routingID").addTextNode("5045075939500000000");
                
                SimpleDateFormat sdf = new SimpleDateFormat("HHmmss");
                String stanStr = sdf.format(new Date());
                //添加 stan
                requestEle.addChildElement("stan").addTextNode(stanStr);
                
                //添加 transactionID
                requestEle.addChildElement("transactionID");
                
                //添加 checkForDuplicate Element
                requestEle.addChildElement("checkForDuplicate").addTextNode("false");
                
                /*
                //  根据要传给mule的参数,创建消息body内容。具体参数的配置可以参照应用集成接易做图术规范1.1版本  
                // =====================================  
               /* SOAPElement bodyElement = body.addChildElement(envelope.createName(  
                        "process", "Request", "http://esb.service.com/"));  
                bodyElement.addChildElement("ServiceCode").addTextNode("10000061");  
                bodyElement.addChildElement("OrigAppId").addTextNode("999");  
                bodyElement.addChildElement("HomeAppId").addTextNode("998");  
                Calendar c = Calendar.getInstance();  
                String reqTime = String.valueOf(c.getTimeInMillis());  
                bodyElement.addChildElement("ReqTime").addTextNode(reqTime);  
                bodyElement.addChildElement("IpAddress").addTextNode("10.212.40.112");  
                bodyElement.addChildElement("OrigSerialNo").addTextNode("201205242011");  
                //(ServiceCode+ OrigAppId+ ReqTime+ IpAddress)签名  
                String AppSignature = "10000061"+"999"+reqTime+"10.212.40.112"+"123456";  
                bodyElement.addChildElement("AppSignature").addTextNode(AppSignature);  
                bodyElement.addChildElement("Version").addTextNode("014");  
    //          bodyElement.addChildElement("RelSessionId").addTextNode("RelSessionId");  
    //          bodyElement.addChildElement("ReplyCode").addTextNode("ReplyCode");  
                bodyElement.addChildElement("ReplyVersion").addTextNode("05");  
                bodyElement.addChildElement("TimeOut").addTextNode("30");  
    //          bodyElement.addChildElement("FtpDir").addTextNode("FtpDir");  
    //          bodyElement.addChildElement("FileList").addTextNode("FileList");  
                bodyElement.addChildElement("serviceParas").addTextNode("<param><name>apptest</name><password>apptest</password></param>");*/  
                // 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端口地址。 
                 */  //https://192.168.38.115:443/svsxml/services/SVSXMLWay
                //"https://webservices.storedvalue.com:443/svsxml/services/SVSXMLWay
                URL url = new URL("https://webservices.storedvalue.com:443/svsxml/services/SVSXMLWay");  
                System.out.println(url);  
                // 响应消息  
                // ===========================================================================  
                SOAPMessage reply = connection.call(message, url);  
                
                
                
                //reply.setProperty(SOAPMessage.CHARACTER_SET_ENCODING, "gb2312");  
                // 打印服务端返回的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("");  
                connection.close();  
                /* 
                 * 模拟客户端A,异常处理测试 
                 */  
              /*  SOAPBody ycBody = reply.getSOAPBody();  
                Node ycResp = ycBody.getFirstChild();  
                System.out.print("returnValue:"+ycResp.getTextContent()); */
                System.out.println(reply.getSOAPPart().getContent().toString());
            } catch (Exception e) {  
                e.printStackTrace();  
                System.out.println(e.getMessage());  
            }  
        }  
    }  
jdk1.4 axis2_1.4 soap api soap消息乱码 https访问 --------------------编程问答-------------------- 帖子不能沉,自己顶 --------------------编程问答-------------------- 我晕,为什么每次发帖都没有人回复。求高手解答
补充:Java ,  Java EE
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,