Java 1.3下的SoapAction问题

时间:2009-06-25 21:43:22

标签: java soap http-headers jdk1.4

这个问题困扰了我好几天。希望有人之前遇到过这种情况并开发了一种解决方法。

所以我开发了一个中间件Java应用程序,在其执行期间调用SOAP动作。现在,1.6 JDK下这段代码的行为运行良好:

        // inside a try-catch block
        SOAPConnectionFactory soapConnectionFactory = 
                     SOAPConnectionFactory.newInstance();
        SOAPConnection connection = 
                  soapConnectionFactory.createConnection();
        MessageFactory messageFactory = MessageFactory.newInstance();          
        SOAPMessage soapMsg = messageFactory.createMessage();
        String soapAction = getHeaderUrl() + "/" + method + "\r\n";
        MimeHeaders headers = soapMsg.getMimeHeaders(); 
        headers.addHeader("SOAPAction", soapAction); 

       // review the SOAPAction header
        int header_count = 
           soapMsg.getMimeHeaders().getHeader("SOAPAction").length;
        out("SOAPActions (from Soap header): ");
        if (header_count == 0) out ("No SOAPActions defined.");
        for (int i=0; i<header_count; i++)
            out("SOAPAction["+i+"] = \"" + 
               soapMsg.getMimeHeaders().getHeader("SOAPAction")[i] 
                  + "\""); 


        SOAPPart soapPart = soapMsg.getSOAPPart();            
        StreamSource s = new StreamSource(new StringReader(msg));
        soapPart.setContent(s);
        soapMsg.saveChanges();
        ByteArrayOutputStream req_bytes = new ByteArrayOutputStream();
        soapMsg.writeTo(req_bytes);
        String req = new String(req_bytes.toByteArray());         
        SOAPMessage reply = connection.call(soapMsg, Url);      

但是,当我在1.3或1.4 JDK下使用这个完全相同的代码并且链接了SOAP库时,上面的所有内容都可以工作/编译/执行,除了标题的SOAPAction字段为空白。奇怪的是,当我检查该标题的值是什么(设置标题后的行)时,会显示相应的值。但是,当它越过电线时,该字段为空。因为这是指示我想要处理器的资源的字段,所以我的消息在门口被转回。

以前有人遇到过这个问题吗?如果是这样,是否有可能的解决方法? (当然,如果需要的话,我愿意使用另一个图书馆。)

更新

我的类路径中的库如下:

  • Xerces(resolver.jar,serializer.jar,xercesImpl.jar,xml-apis.jar)
  • Saaj(saaj.jar)
  • Axis(axis.jar
  • JAX-RPC(jaxrpc.jar)
  • 标准SOAP(activation.jar,mail.jar,soap.jar,xerces.jar)
  • Commons logging and discovery

提前致谢!

2 个答案:

答案 0 :(得分:0)

特有。你说你在SOAP库中链接 - 究竟是哪些?

尝试使用不同的SOAP堆栈(例如CXF或JAX-WS-RI)我不会受到伤害,但如果这两者中的任何一个支持java 1.3,我会感到惊讶。

答案 1 :(得分:0)

看起来这实际上可能是疏忽或错误,但无论如何我的解决方法是手动处理较低级别的HTTP内容,即:

        URL u = new URL(Url);
        URLConnection uc = u.openConnection();
        HttpURLConnection  conn = (HttpURLConnection) uc;

        conn.setDoOutput(true);
        conn.setDoInput(true);
        conn.setRequestMethod("POST");
        conn.setRequestProperty("SOAPAction", getHeaderUrl() + "/" + method);
        conn.setRequestProperty("Content-type", "text/xml");

        OutputStream out = conn.getOutputStream();
        Writer wr = new OutputStreamWriter(out);

        wr.write(msg);
        wr.flush();
        wr.close();

        InputStream in = conn.getInputStream();
        String rsp="";
        int c;
        while ((c = in.read()) != -1) { 
            System.out.write(c);
            rsp = rsp.concat(String.valueOf((char)c));
        }
        in.close();
        rsp = deSOAP(rsp);
        response = response.fromXML(rsp);
        return response;

事实证明,这比基于SOAP的解决方案更简洁。