DBMNG数据库管理与应用

抓住自己最有兴趣的东西,由浅入深,循序渐进地学……
当前位置:首页 > 经验分享 > Java组件

Axis2 java调用.net webservice接口的问题

这是一个古老的问题,古老到从我若干年前遇到这样的问题就是一个解决之道:反复尝试。其实标准是什么,标准就是一个束缚,一种按既定规则的束缚,错点点,你的调用就可能不成功,不成功后你要花费大量的力气查找原因和错误,差异很多帖子,查找相似的地方,Webservice的实现不同,Soap,CXF,Axis等,每种工具都有指定的方式,刚开始尝试Soap发现这个根本没合适的包进行调用,也是IBM比较老的jar,2001年写的比较复杂,我对比较复杂的东西向来不感兴趣,因为太复杂我也搞不懂。索性用Axis2,在调用之前你要知道soap的两个协议版本1.1,1.2是不太一样的。

问题一:org.apache.axis2.AxisFault: 服务器未能识别 HTTP 头 SOAPAction 的值


这个错误错误查了好久,最后发现对方给的资料里面忘记给命名空间地址了,我去真是头疼,注意的是需要加方法名称


http://tempuri.org/GetSign


问题二:org.apache.axis2.AxisFault: 服务器无法处理请求。 ---> 未将对象引用设置到对象的实例。

这个错误,也是满天飞的帖子其实是一个很小的问题,反斜杠


OMNamespace omNs = fac.createOMNamespace("http://tempuri.org/", "");


标准吧,只要错那么点点,你就别想调通了,我把整个代码示例贴到下面:

import org.apache.axiom.om.OMAbstractFactory;

import org.apache.axiom.om.OMElement;

import org.apache.axiom.om.OMFactory;

import org.apache.axiom.om.OMNamespace;

import org.apache.axis2.AxisFault;

import org.apache.axis2.addressing.EndpointReference;

import org.apache.axis2.client.Options;

import org.apache.axis2.client.ServiceClient;

import org.apache.axis2.transport.http.HTTPConstants;

 

 

public class SoapAxis {

     private static EndpointReference targetEPR = new EndpointReference("http://192.168.0.185/OnlinePaywebservice/platformws.asmx");

public static void main(String[] args) {

        Options options = new Options();

        options.setAction("http://tempuri.org/GetSign");// 调用接口方法

        options.setTo(targetEPR);

        options.setProperty(HTTPConstants.CHUNKED, "false");//设置不受限制.

 

 

        ServiceClient sender = null;

        try {

             sender = new ServiceClient();

             sender.setOptions(options);

             OMFactory fac = OMAbstractFactory.getOMFactory();

             OMNamespace omNs = fac.createOMNamespace("http://tempuri.org/", "");

             OMElement method = fac.createOMElement("GetSign", omNs);

             OMElement name = fac.createOMElement("prestr", omNs);// 设置入参名称

             OMElement name2 = fac.createOMElement("key", omNs);// 设置入参名称

             name.setText("hawei");// 设置入参值

             name2.setText("6181a1fb89564b589283ad578baa7d5e");

             method.addChild(name);

             method.addChild(name2);

             method.build();

             System.out.println("method:" + method.toString());

             OMElement response = sender.sendReceive(method);

             System.out.println("response:" + response);

             OMElement elementReturn = response.getFirstElement();

             System.out.println("cityCode:" + elementReturn.getText());

          } catch (AxisFault e) {

             System.out.println("Error");

               e.printStackTrace();

          }

       }

}



问题三:org.apache.axis2.AxisFault: 服务器无法读取请求。 ---> XML 文档(1, 291)中有错误。 ---> 字符串“2014-12-09 02:03:00”不是有效的 AllXsd 值。


这个问题主要是日期格式问题,日期格式改成“2014-12-09T02:03:00”


String timestamp1 =new SimpleDateFormat("yyyy-MM-dd").format(nowDate);

String timestamp2 =new SimpleDateFormat("hh:mm:ss").format(nowDate);

String timestampString=timestamp1+" "+timestamp2;


问题四:SOAP头如何加

        public static void addValidation(ServiceClient serviceClient) {

OMFactory fac = OMAbstractFactory.getOMFactory();

OMNamespace omNs = fac.createOMNamespace(tns, "");

OMElement header = fac.createOMElement("CredentialSoapHeader", omNs);

OMElement appId = fac.createOMElement("AppID", omNs);

//

appId.setText("145");

header.addChild(appId);

System.out.println("header:" + header.toString());

serviceClient.addHeader(header);

}



下面罗列下需要的Axis2的包,包实在太多,不要都打入进来


本站文章内容,部分来自于互联网,若侵犯了您的权益,请致邮件chuanghui423#sohu.com(请将#换为@)联系,我们会尽快核实后删除。
Copyright © 2006-2023 DBMNG.COM All Rights Reserved. Powered by DEVSOARTECH            豫ICP备11002312号-2

豫公网安备 41010502002439号