2012年6月6日 星期三

Axis client for .Net web service

這次真是太雖小了Orz..居然用到貌似有bug的前期版本~
問題是~~
SimpleDeserializer encountered a child element, which is NOT expected, in something it was trying to deserialize.
發生的原因應該是在client invoke預設axis他去check 參數傳遞時~ 對於不需要傳參數的判斷會有問題..(明明就是不用~ 但他堅持一定要讀一個來用Orz)

不過因為OOXX機車的原因..所以也不能換lib..
網路上大家好像也沒特別講這個問題要怎麼解...
終於試到一個invoke的方式可以繞過那一段了T_T..要是繞不過去~就要硬刻一個http request了Orz..簡單的說就是用Message來發..這樣子他就不會去check參數了~ 還可以刻一個美美的xml不會有ns:前綴..

使用method如下~

            Call client = new Call(url);
            client.setUseSOAPAction(true);
            String uri = namespace;
            if(uri.endsWith("/"))  {
                uri += op;
            }  else  {
                uri += "/"+op;
            }
            client.setSOAPActionURI(uri);
            if(timeout > 0)  {
                client.setTimeout(timeout);
            }
            client.setOperationName(new QName(namespace,op));
            String xml ="";
            xml += "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">";
            xml += "<soap:Body>";
            if(params == null || params.size() == 0)  {
                xml += "<"+op+" xmlns=\""+namespace+"\" />";
            }  else  {
                xml += "<"+op+" xmlns=\""+namespace+"\" >";
                for(int i = 0 ; i < params.size() ; i++)  {
                    String[] pair = params.get(i);
                    xml +="<"+pair[0]+">"+pair[1]+"</"+pair[0]+">";
                }
                xml += "</"+op+">";
            }
            xml += "</soap:Body>";
            xml += "</soap:Envelope>";          
          
            Message msg = new Message(xml);
            SOAPEnvelope res = client.invoke(msg);          
           
 
若是使用RPCElement(之前的寫法)~ 則只能處理有帶參數的~節錄如下當參考
在axis裡~好像前綴是拿不掉的(不過至少.net那邊的web service看得懂就好..)~axis2可以偷做手腳拿掉就是..

            Call client = new Call(url);
            client.setUseSOAPAction(true);
            String uri = namespace;
            if(uri.endsWith("/"))  {
                uri += op;
            }  else  {
                uri += "/"+op;
            }
            client.setSOAPActionURI(uri);
            if(timeout > 0)  {
                client.setTimeout(timeout);
            }

            RPCElement req = new RPCElement(op);
            req.setNamespaceURI(namespace);
            req.setAttribute("xmlns", namespace);

            for(int i = 0 ; i < params.size() ; i++)  {
                String[] input = params.get(i);
                if(input.length == 2 && input[0] != null && input[0].length() > 0)  {
                    RPCElement ele = new RPCElement(input[0]);
                    if(input[1] != null)  {
                        ele.addTextNode(input[1]);
                    }
                    req.addChildElement(ele);
                }
            }
            re = (String)client.invoke( req );

沒有留言: