【问题标题】:Consuming java web service through .net - data inconsistence通过 .net 使用 Java Web 服务 - 数据不一致
【发布时间】:2013-12-24 11:47:50
【问题描述】:

我有一个 Java 项目(一个投票系统),我在其中实现了一个 Web 服务。

我的 getResults 方法返回一个 String[]。如果投票完成,则该方法返回填充有“项目 1 - 2 票,项目 2 - 3 票...”的数组。如果不是,则返回带有单个字符串的数组,表示投票仍在进行中。

问题是,如果我从我的 java 应用程序中调用 getResults,它会按预期工作,但如果我从我的 web 服务调用它,它总是返回投票仍在进行,而不是结果。

我正在通过一个 c# 控制台应用程序使用这个 Web 服务,它是 Visual Studio。

我对网络服务很陌生,所以让我问一下。当我像这样实例化我的服务时:

ServerService ss = new ServerService();

它是在我的 Java 应用程序中创建我的类 Server() 的新实例,还是只是连接到我当前实例的一种方式?

我希望我已经很好地解释了我的问题,希望你能帮助我。

谢谢,圣诞快乐 :)

编辑:

这是 Web 服务访问的方法

public String[] getResults() throws RemoteException {
    if (ended) {
        return results.toArray(new String[results.size()]);
    } else {
        ArrayList<String> temp = new ArrayList<String>();
        temp.add("Voting is still on");
        return temp.toArray(new String[temp.size()]);
    }
}

编辑 2:

WSDL:

<wsdl:definitions targetNamespace="http://backend.ve"
xmlns:apachesoap="http://xml.apache.org/xml-soap" xmlns:impl="http://backend.ve" xmlns:intf="http://backend.ve" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:wsdlsoap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<!--WSDL created by Apache Axis version: 1.4
Built on Apr 22, 2006 (06:55:48 PDT)-->
 <wsdl:types>
  <schema elementFormDefault="qualified" targetNamespace="http://backend.ve" xmlns="http://www.w3.org/2001/XMLSchema">
   <element name="getResults">
    <complexType/>
   </element>
   <element name="getResultsResponse">
    <complexType>
     <sequence>
      <element maxOccurs="unbounded" name="getResultsReturn" type="xsd:string"/>
     </sequence>
    </complexType>
   </element>
  </schema>
 </wsdl:types>
   <wsdl:message name="getResultsRequest">
      <wsdl:part element="impl:getResults" name="parameters">
      </wsdl:part>
   </wsdl:message>
   <wsdl:message name="getResultsResponse">
      <wsdl:part element="impl:getResultsResponse" name="parameters">
      </wsdl:part>
   </wsdl:message>
   <wsdl:portType name="Server">
      <wsdl:operation name="getResults">
         <wsdl:input message="impl:getResultsRequest" name="getResultsRequest">
       </wsdl:input>
         <wsdl:output message="impl:getResultsResponse" name="getResultsResponse">
       </wsdl:output>
      </wsdl:operation>
   </wsdl:portType>
   <wsdl:binding name="ServerSoapBinding" type="impl:Server">
      <wsdlsoap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
      <wsdl:operation name="getResults">
         <wsdlsoap:operation soapAction=""/>
         <wsdl:input name="getResultsRequest">
            <wsdlsoap:body use="literal"/>
         </wsdl:input>
         <wsdl:output name="getResultsResponse">
            <wsdlsoap:body use="literal"/>
         </wsdl:output>
      </wsdl:operation>
   </wsdl:binding>
   <wsdl:service name="ServerService">
      <wsdl:port binding="impl:ServerSoapBinding" name="Server">
         <wsdlsoap:address location="http://localhost:8080/VE/services/Server"/>
      </wsdl:port>
   </wsdl:service>
</wsdl:definitions>

C#:

namespace WSTest
{
    class Program
    {
        static void Main(string[] args)
        {
            ServerService ss = new ServerService();
            foreach (String s in ss.getResults())
            {
                Console.WriteLine(s);
            }
            Console.ReadLine();
        }
    }
}

【问题讨论】:

  • 代码行:ServerService ss = new ServerService(); 创建代理类的新对象,该对象是在您添加服务引用时在 Visual Studio 应用程序中创建的.现在这个对象会调用你的 webmethod。你可以发布你的服务的一部分吗?方法不止一种吗?
  • 在我的帖子中添加了 webmethod,需要看 wsdl 吗?
  • 是的,还有其他一些事情:1. 结束值(我猜这必须与调用者无关) 2. WSDL 3. 调用此服务的 C# 代码。 4.绑定细节等
  • 见上面的 wsdl 和 c# 代码。如果投票已经结束(有时间限制),则为真,如果仍在进行,则为假。我认为问题不在 c# 代码中,因为我已经使用 eclipse 集成的 Web 服务工具对其进行了测试,并且发生了同样的事情

标签: c# java .net web-services


【解决方案1】:

它只会为您的服务实例化一个 C# 代理 - 一个由 .NET 自动生成的类,其中包含用于管理与您的服务的通信的所有幕后代码。

我建议您在尝试将其与 .NET 集成之前使用 SoapUI 之类的工具测试您的服务,看看您是否有同样的问题。

【讨论】:

  • 使用 Eclipse 工具“使用 Web 服务资源管理器测试”对其进行了测试,并且发生了同样的事情,所以问题应该出在我的 java 应用程序中
【解决方案2】:

我无法找出您代码中的确切问题,但这里有一些提示可以帮助您解决问题。

  1. 您的方法没有返回字符串,请参阅 WSDL:
<wsdl:operation name="getResults">
     <wsdl:input message="impl:getResultsRequest" name="getResultsRequest">
   </wsdl:input>
     <wsdl:output message="impl:getResultsResponse" name="getResultsResponse">
   </wsdl:output>
  </wsdl:operation>

但您希望字符串作为 C# 代码中的输出。

2 。您需要在 C# 端创建两个对象,即:getResultsRequest 和 getResultsResponse

我知道您在 Eclipse 中遇到了同样的错误,但是当您从 Java 客户端调用它时,您的服务可以正常工作。所以你的服务逻辑是正确的。现在唯一可能出现问题的地方是您的服务和客户端之间的通信。

所以我建议的 C# 代码是:

namespace WSTest
{
 class Program
 {
    static void Main(string[] args)
    {
        ServerService ss = new ServerService();
        getResultRequest request = new getResultRequest();
        getResultResponse response = new getResultResponse();
        response = ss.getResultResponse(request);
        // Do something with response.
        Console.ReadLine();
    }
 }
}

【讨论】:

  • 我认为问题与 end 变量有关,因为它是一个全局变量,因为我需要在各种方法中使用它。我已经改变了我的方法,用一个局部变量做一个简单的 if else ,它工作正常。我猜网络访问的方法不能使用全局变量?有什么办法可以解决吗?
  • 最好避免使用全局变量。如果您想跨应用程序访问某些值,请创建一个没有设置器的属性。这样您可以确保没有人更改您的值。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-07-24
  • 1970-01-01
  • 1970-01-01
  • 2011-07-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多