zoukankan      html  css  js  c++  java
  • Axis2 java调用.net webservice接口的问题(郑州就维)

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

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

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

    [html] view plaincopy
     
    1. http://tempuri.org/GetSign  


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

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

    [html] view plaincopy
     
    1. OMNamespace omNs = fac.createOMNamespace("http://tempuri.org/", "");  


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

    [java] view plaincopy
     
    1. import org.apache.axiom.om.OMAbstractFactory;  
    2. import org.apache.axiom.om.OMElement;  
    3. import org.apache.axiom.om.OMFactory;  
    4. import org.apache.axiom.om.OMNamespace;  
    5. import org.apache.axis2.AxisFault;  
    6. import org.apache.axis2.addressing.EndpointReference;  
    7. import org.apache.axis2.client.Options;  
    8. import org.apache.axis2.client.ServiceClient;  
    9. import org.apache.axis2.transport.http.HTTPConstants;  
    10.   
    11.   
    12. public class SoapAxis {  
    13.      private static EndpointReference targetEPR = new EndpointReference("http://192.168.0.185/OnlinePaywebservice/platformws.asmx");  
    14.     public static void main(String[] args) {  
    15.         Options options = new Options();  
    16.         options.setAction("http://tempuri.org/GetSign");// 调用接口方法  
    17.         options.setTo(targetEPR);  
    18.         options.setProperty(HTTPConstants.CHUNKED, "false");//设置不受限制.  
    19.   
    20.   
    21.         ServiceClient sender = null;  
    22.         try {  
    23.              sender = new ServiceClient();  
    24.              sender.setOptions(options);  
    25.              OMFactory fac = OMAbstractFactory.getOMFactory();  
    26.              OMNamespace omNs = fac.createOMNamespace("http://tempuri.org/", "");  
    27.              OMElement method = fac.createOMElement("GetSign", omNs);  
    28.              OMElement name = fac.createOMElement("prestr", omNs);// 设置入参名称  
    29.              OMElement name2 = fac.createOMElement("key", omNs);// 设置入参名称  
    30.              name.setText("hawei");// 设置入参值  
    31.              name2.setText("6181a1fb89564b589283ad578baa7d5e");  
    32.              method.addChild(name);  
    33.              method.addChild(name2);  
    34.              method.build();  
    35.              System.out.println("method:" + method.toString());  
    36.              OMElement response = sender.sendReceive(method);  
    37.              System.out.println("response:" + response);  
    38.              OMElement elementReturn = response.getFirstElement();  
    39.              System.out.println("cityCode:" + elementReturn.getText());  
    40.           } catch (AxisFault e) {  
    41.              System.out.println("Error");  
    42.                e.printStackTrace();  
    43.           }  
    44.        }  
    45. }  

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

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

    [html] view plaincopy
     
    1. String timestamp1 =new SimpleDateFormat("yyyy-MM-dd").format(nowDate);  
    2. String timestamp2 =new SimpleDateFormat("hh:mm:ss").format(nowDate);  
    3. String timestampString=timestamp1+" "+timestamp2;  


    问题四:SOAP头如何加

    [html] view plaincopy
     
    1.        public static void addValidation(ServiceClient serviceClient) {  
    2.     OMFactory fac = OMAbstractFactory.getOMFactory();  
    3.     OMNamespace omNs = fac.createOMNamespace(tns, "");  
    4.     OMElement header = fac.createOMElement("CredentialSoapHeader", omNs);  
    5.     OMElement appId = fac.createOMElement("AppID", omNs);  
    6.     //  
    7.     appId.setText("145");  
    8.     header.addChild(appId);  
    9.     System.out.println("header:" + header.toString());  
    10.     serviceClient.addHeader(header);  
    11. }  



     

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

  • 相关阅读:
    高可用的MongoDB集群-实战篇
    【转载】特征选择常用算法综述
    【转载】Pytorch tutorial 之Datar Loading and Processing
    论文笔记系列-Well Begun Is Half Done:Generating High-Quality Seeds for Automatic Image Dataset Construction from Web
    重新定义Pytorch中的TensorDataset,可实现transforms
    【转载】机器学习之特征工程
    Pytorch里的CrossEntropyLoss详解
    Python日志模块logging用法
    【转载】如何进行数据变换
    【转载】使用sklearn优雅地进行数据挖掘
  • 原文地址:https://www.cnblogs.com/Alex80/p/5058189.html
Copyright © 2011-2022 走看看