When you try invoke a Java/Axis Web Service from a proxy class generated by Visual Studio 2005 or Visual Studio 2008 you often crash against the ‘return null’ issue.

The web service seems to get called correctly and it responds to your client in the right way (you have no exception of any sort), but your returned object is null, it happened to me to face this situation today for the first time, there are a couple of things you can do to debug and resolve this situation:

  • Let’s consider our function call:
       1: [Test]
       2: public void T1()
       3: {
       4:     Test.TestWs ws = new AxisWebService.Test.TestWs ();
       5:     Test.State[] arr = ws.getStates(1);
       6:     Assert.IsNotNull(arr);
       7: }

here we expect to have back an array of State objects, instead we obtain the hated ‘null’.

  • The first thing to do is to download and install ‘Fiddler’ (do it right now if you don’t have it already) and have a look at what the web service respond to us (the snippet is a trimmed response):
       1: HTTP/1.1 200 OK
       2: Connection: close
       3: Date: Wed, 15 Oct 2008 12:36:26 GMT
       4: Server: Microsoft-IIS/6.0
       5: X-Powered-By: ASP.NET
       6: Content-Type: text/xml;charset=utf-8
       7: 
       8: <?xml version="1.0" encoding="utf-8"?>
       9: <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
      10: <soapenv:Body>
      11: <getStatesResponse xmlns="">
      12: <item xsi:type="ns1:State" xmlns:ns1="http://mynamespace.it/">
      13: <Code>A001</Code>
      14: <Description>Test</Description>
      15: </item>
      16: </getStatesResponse>
      17: </soapenv:Body>
      18: </soapenv:Envelope>

Since the web service is responding correctly the problem is in the deserialization stage of the data stream sent back by the web service.

  • It’s time to show some hidden file of the solution and look inside the ‘reference.cs’ (this is the default file in which Visual Studio creates some proxy classes).

Looking at the proxy classes generated by Visual Studio, it seems that we have all that we need: a class to call the web service and series of classes that map the objects the service returns; where’s the problem then? it turns out that the web service client can’t understand the response stream, so the problem is in a mismatch somewhere.

Given the fact we have all the classes and all of them have the right properties, it’s time to look for the namespaces:

  • Visual Studio 2005
       1: [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://mynamespace.it/getStates",
       2:     Use = System.Web.Services.Description.SoapBindingUse.Literal,
       3:     ParameterStyle = System.Web.Services.Protocols.SoapParameterStyle.Bare)]
       4: [return: System.Xml.Serialization.XmlArrayAttribute("getStatesResponse", Namespace = "http://mynamespace.it/")]
       5: [return: System.Xml.Serialization.XmlArrayItemAttribute("item", Form = System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable = false)]
       6: public Stato[] getStates([System.Xml.Serialization.XmlElementAttribute(Namespace = "http://mynamespace.it/")] int getStatesRequest)
       7: {
       8:     object[] results = this.Invoke("getStates", new object[] {
       9:                 getStatesRequest});
      10:     return ((State[])(results[0]));
      11: }

pay attention to line number 4:

1: [return: System.Xml.Serialization.XmlArrayAttribute("getStatesResponse", Namespace = "http://mynamespace.it/")]

here it states that the getResponseState element is qualified with the ‘http://mynamespace.it'/’ namespace...but look at what Fiddler captured for us (line 11 of the previous snippet): there we see that the namespace associated with the element is “” (empty string), so here it is our mismatch. To fix the problem you have to manually edit the attribute and correct the namespace to “” (empty string).

Be very careful: writing Namespace = “” or removing it at all are two completely different things.

Having made this fix our test passes and we are able to get our objects back from the web service.

  • Visual Studio 2008 
    It produces a completely different set of classes to call the web service, we have an interface that describes the service, a series of classes that represent the request and response of each method exposed by the interface and finally we have the proxy classes for the objects returned. We know that the problem is at the ‘client’ side so checking the request classes is useless, we focus our attention on the response classes and on object classes to verify the namespace mappings:
       1: [System.Diagnostics.DebuggerStepThroughAttribute()]
       2: [System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "3.0.0.0")]
       3: [System.ServiceModel.MessageContractAttribute(IsWrapped=false)]
       4: public partial class getStatesResponse {
       5:    
       6:     [System.ServiceModel.MessageBodyMemberAttribute(Name="getStatesResponse", Namespace="http://mynamespace.it/", Order=0)]
       7:     [System.Xml.Serialization.XmlArrayItemAttribute("item", Form=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable=false)]
       8:     public State[] getStatesResponse1;
       9:    
      10:     public getStatesResponse() {
      11:     }
      12:    
      13:     public getStatesResponse(State[] getStatesResponse1) {
      14:         this.getStatesResponse1 = getStatesResponse1;
      15:     }
      16: }

look at line 6, you can see a namespace mismatch again, the fix is the same applied before.

In the end, if you are using a Java/Axis Web Service and you get null results from you service calls, don't trust the auto-generated proxy classes too much and check that the attribute that defines the namespace for each object match what you get from the wsdl and from the traced response.

引用地址:<http://www.primordialcode.com/blog/post/invoking-javaaxis-web-service-net-return-null-issue> 

转载于:https://www.cnblogs.com/itpro/p/5320170.html

调用java的webservice返回null相关推荐

  1. .NET调用JAVA的WebService方法

    调用WebService,最简单的办法当然是直接添加WEB引用,然后自动产生代理类,但是在调用JAVA的WebService时并没有这么简单,特别是对于SoapHeader的处理,在网上也有相关资料, ...

  2. C#调用Java的WebService时添加身法认证信息的方法

    Java生成的WebService要求在调用时在http的header里必须带上Authorization:Basic xxxxxxxxxxxxxxx的认证信息 因为是在添加Web Reference ...

  3. 水晶易表调用C#的WebService,返回数据集合

    1. 水晶易表不能识别WS接口返回的DataTable或DataSet数据类型,会提示"无法加载URL" 3. C#调用Oracle的Package,并返回数据列表 2. 经查证, ...

  4. axis调用java实现webservice实例

    一: 首先创建个WEB工程,然后: http://ws.Apache.org/axis/网站下载Axis安装包.当然还依赖其他包的,我这里在附件里上传了所有应用到得包,方便大家. 二: 然后就写wsd ...

  5. Linux下使用Curl调用Java的WebService接口

    其实只要是标准的WSDL的SOA接口WebService都可以用. 调用方式: 注意:上面的方式不包括加密或者登录的,其实SOA有一套完整的加密方式. curl -H'Content-Type: te ...

  6. C# 调用Java接口

    最近工作任务中包含了系统之间数据的互通,当然就考虑系统互相开通接口来实现通信了! 作为.NET的开发者,还没有调用过Java接口的经历,惭愧惭愧! 话不多说,直接进入正题! 调用webservice接 ...

  7. CORBA 简单了解和JAVA与C++互操以及C++调用Java web service

    CORBA了解 CORBA(Common Object Request Broker Architecture, 公共对象请求代理体系结构)是由OMG(对象管理组织,Object Management ...

  8. 记录一次json_decode 返回NULL解决过程

    后台返回数据,前端收到数据之后,在调用json_decode 之后,返回null. 用json_last_error() 报错 3 在解码前,对字符串进行处理 $data = preg_replace ...

  9. java中secretkey,java-在SecretKey上调用.getEncoded()返回null

    我使用以下代码生成AES密钥: KeyGenParameterSpec.Builder builder = new KeyGenParameterSpec.Builder("db_enc_k ...

  10. java 解析http返回的xml_Java解析调用webservice服务的返回XML串详解

    本文由Markdown语法编辑器编辑完成. 1. 需求分析: 已知当在调用某一webservice的服务时,如果调用成功,会接受到该服务的返回XML串.后端在获取了该XML原始串时,需要进行解析,将其 ...

最新文章

  1. RDKit | 基于主成分分析可视化(DrugBank)类药性的化学空间
  2. 浅析:seo工程师擅长的网站数据分析
  3. 如何编译ReactNative示例程序Examples
  4. struts2 property标签的使用技巧
  5. 最受欢迎的Java环境
  6. jenkins的svn路径中文问题
  7. Java程序强制删除文件
  8. SQLSERVER dbo 解释
  9. 温暖和暖和的区别Java_国内冬天哪里比较暖和 冬天暖和的城市排名
  10. 玩游戏计算机缺失msvcp140,游戏缺少msvcp140.dll错误提示怎么办解决方法
  11. 做“合规”的数据处理者 | 一文图解《网络数据安全管理条例》
  12. codeforces1132E Knapsack
  13. 开单大师目录结构学习
  14. Oracle Database 11g Release 2认证支持的操作系统版本跨度很大
  15. [cesium] 基于Cesium的动态泛光效果示例
  16. 团灭了3个月的线下营销,还有希望吗?
  17. 你的性格是什么颜色——记录乐嘉的性格色彩分析(附性格色彩自测题
  18. 生产者消费者ReentrantLock实现以及BlockingQueue实现
  19. 053试题 158/449/637 - Scheduler Window
  20. 20世纪最好的10个算法(转)

热门文章

  1. soultion of mySQL disk exceeded problem
  2. leetcode 175. Combine Two Tables
  3. sendfile()对nginx性能的提升
  4. 构建一个可靠的分布式计数器--memcached之incr/decr操作实战分析
  5. HIVE 分区 分桶
  6. JS和JS是IE上JavaScript或JScript的缩写。
  7. 搭建自己的博客(二十):优化博客评论功能
  8. Python基础之字典
  9. 【Luogu3478】【POI2008】STA-Station(动态规划)
  10. iPhone唯一标识符