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

调用java的webservice返回null的更多相关文章

  1. .NET调用JAVA的WebService方法

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

  2. Delphi调用JAVA的WebService上传XML文件(XE10.2+WIN764)

    相关资料:1.http://blog.csdn.net/luojianfeng/article/details/512198902.http://blog.csdn.net/avsuper/artic ...

  3. delphi7编写客户端调用java服务器端webservice示例

    1. 首先取得java-webservice服务器端地址.我的是:http://localhost:8080/mywebservice/services/mywebservice?wsdl 2. 然后 ...

  4. Delphi动态调用Java的WebService 转

    Delphi动态调用Java的WebService —— 基于“Axis2发布WebService例子(HelloWorld)” uses ComObj; var WsObject: Variant; ...

  5. C#调用Java的WebService添加SOAPHeader验证(2)

    C#调用Java的WebService添加SOAPHeader验证 上一篇链接如上,更像是 Net下采用GET/POST/SOAP方式动态调用WebService的简易灵活方法(C#) 来处理xml, ...

  6. C#调用Java的WebService添加SOAPHeader验证

    C#调用Java的WebService添加SOAPHeader验证(2) 1.问题描述 调用的Java的webservice string Invoke(string func, string req ...

  7. c#调用JAVA的Webservice处理XML数据及批量轮询的实现方法

    前段时间做一个调用外单位WEBSERVICE的项目,项目完成的功能其实很简单,就是我们单位有很多车友会员,我们想对他们提供车辆违章信息告之服务!我们这边交警部门给我们开放了WS的接口,我们就是想通过这 ...

  8. PHP调用JAVA的WebService简单实例

    使用PHP调用JAVA语言开发的WebService.客户端提交两个String类型的参数,服务端返回一个对象类型.服务端使用AXIS-1.4作为SOAP引擎.客户端为PHP5.2.9,使用NuSOA ...

  9. C#调用Java的WebService出现500 服务器错误

    最近在用C#调用Java写的WebService时,发现老是返回500 服务器错误,到底什么原因一直找不出来, 后来google了以后,找到国外的http://stackoverflow.com站点已 ...

随机推荐

  1. html2canvas 网页截图 下载 上传

    利用html2canvas插件 对网页截图 并下载和上传图片. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//E ...

  2. 在生成 Visual c + + 2005年或从 DLL 文件中使用 CString 派生的类的 Visual c + +.net 应用程序时,您可能会收到 LNK2019 错误消息

    http://support.microsoft.com/kb/309801

  3. 《Genesis-3D开源游戏引擎完整实例教程-跑酷游戏篇01:道路的自动生成》

    1.道路的自动生成 道路自动生成概述: 3D跑酷游戏的核心就是跑,在跑这一过程中增加趣味性使得游戏具有更多的可玩性.道路的自动生成和自由拼接,为游戏增设了更多的不可预见性.这种不可预见性使得玩家在游戏 ...

  4. 数组(Array)

    1. 数组(Array):相同类型数据的集合就叫做数组. 2. 数组的定义与赋值(系统会默认初始化) 普通数组: package com.li; public class Array{ public ...

  5. [读书笔记]了不起的node.js(二)

    这周做项目做得比较散(应该说一直都是这样),总结就依据不同情境双开吧-这篇记录的是关于node的学习总结,而下一篇是做项目学到的web前端的知识. 1.HTTP篇 node的HTTP模块在第一篇时接触 ...

  6. Java语言速览:StackOverflow

    关于 java Java(请不要与 JavaScript 搞混)是一种设计为与 Java 虚拟机 (JVM) 一起使用的多用途编程语言.一般将安装了相关工具可以开发并运行 Java 程序的电脑系统称为 ...

  7. (5)RARP:逆地址解析协议

    一.简介 无盘系统的RARP实现过程是从接口卡上读取唯一的硬件地址,然后发送一份RARP请求(一帧在网络上广播的数据),请求某个主机响应该无盘系统的IP地址(在RARP应答中).感觉这个过程和上一章中 ...

  8. cocos2d-x 3.0 开发(一) Hello_New_World

    1.Previous On 2dx     2dx 的3.0版本是个与以往不同的版本.变化比2dx从1.x 到2.x的变化还要大不少.具体的新功能可以参见:CocoaChina大会见闻——cocos2 ...

  9. 3.x的触摸响应机制

    第一种是采用函数回调,主要是用于MenuItem [cpp] view plaincopy // a selector callback void menuCloseCallback(Object*  ...

  10. zigbee 学习记录之一:资料搜索

    先从网络来一段资料吧,在学习过程中慢慢整理资料. <由于zigbee 以cc2530 51单片机为基础,Zstack为源头,比较成熟了,学习和摸索就从Zstack开始吧.没有师傅,就有从网络上大 ...