从 .Net 项目调用 Java Web 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5383858/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 10:52:20  来源:igfitidea点击:

Calling a Java Web Service from a .Net Project

c#java.netweb-services

提问by Bob Avallone

I hoping you guys can help me.

我希望你们能帮助我。

I am a .Net developer using C# code. I have been given the task to call a web service written in Java by my colleague. The first problem we have is he doesn't speak .Net and I don't speak Java.

我是使用 C# 代码的 .Net 开发人员。我的同事给我分配了调用用 Java 编写的 Web 服务的任务。我们遇到的第一个问题是他不会说 .Net 而我不会说 Java。

The first question is whether to using a web reference or a service reference. Both seem to work in the sense that my program can find the service and add the reference. But the Service Reference is not exposed in my project. For this reason I went with the Web reference at least for now.

第一个问题是使用网络引用还是服务引用。在我的程序可以找到服务并添加引用的意义上,两者似乎都有效。但是我的项目中没有公开服务引用。出于这个原因,我至少现在使用了 Web 参考。

I can set any of the properties and recall them and it works fine. The question I have is how to invoke the method. When I ask my colleague he does not seem to get what I am talking about.

我可以设置任何属性并调用它们并且它工作正常。我的问题是如何调用该方法。当我问我的同事时,他似乎不明白我在说什么。

I need the answer if it's something simply or if I need to ask a question my colleague in a way that a Java person will understand what I'm talking about.

如果只是简单的事情,或者如果我需要以 Java 人员能够理解我在说什么的方式向我的同事提问,我需要答案。

Here is the code I have written:

这是我写的代码:

WebReference.getContactUs myContactUs = new WebReference.getContactUs();

        myContactUs.ContactUsReceived = DateTime.Now;
        myContactUs.FirstName = "Bob";
        myContactUs.LastName = "Avallone";
        DateTime _ThisDateTime = myContactUs.ContactUsReceived;

Here is the code from the wsdl

这是来自 wsdl 的代码

<?xml version="1.0" encoding="utf-8"?>
<definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://contactus.nrcme.fmcsa.dot.gov/" xmlns:ns1="http://nrcme.fmcsa.dot.gov/ContactUs/" name="ContactUs" targetNamespace="http://nrcme.fmcsa.dot.gov/ContactUs/" xmlns="http://schemas.xmlsoap.org/wsdl/">
  <types>
    <xs:schema targetNamespace="http://nrcme.fmcsa.dot.gov/ContactUs/" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
      <xs:element name="getContactUs">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="ContactUsReceived" type="xs:dateTime" />
            <xs:element name="FirstName" type="xs:string" />
            <xs:element name="LastName" type="xs:string" />
            <xs:element name="Title" type="xs:string" />
            <xs:element name="Organization" type="xs:string" />
            <xs:element name="Address1" type="xs:string" />
            <xs:element name="Address2" type="xs:string" />
            <xs:element name="City" type="xs:string" />
            <xs:element name="State" type="xs:string" />
            <xs:element name="ZipCode" type="xs:string" />
            <xs:element name="Telephone" type="xs:string" />
            <xs:element minOccurs="0" name="Fax" type="xs:string" />
            <xs:element name="Email" type="xs:string" />
            <xs:element name="EmailConfirm" type="xs:string" />
            <xs:element minOccurs="0" name="Comment" type="xs:string" />
            <xs:element minOccurs="0" name="Question1" type="xs:string" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
      <xs:element name="getContactUsResponse">
        <xs:complexType>
          <xs:sequence>
            <xs:element name="ContactUsReceived" type="xs:dateTime" />
            <xs:element minOccurs="0" name="status" type="xs:string" />
          </xs:sequence>
        </xs:complexType>
      </xs:element>
    </xs:schema>
  </types>
  <message name="ContactUs_getContactUsResponse">
    <part name="getContactUsResponse" element="ns1:getContactUsResponse" />
  </message>
  <message name="ContactUs_getContactUs">
    <part name="getContactUs" element="ns1:getContactUs" />
  </message>
  <portType name="ContactUs">
    <operation name="getContactUs" parameterOrder="getContactUs">
      <input message="ns1:ContactUs_getContactUs" />
      <output message="ns1:ContactUs_getContactUsResponse" />
    </operation>
  </portType>
  <binding name="ContactUsBinding" type="ns1:ContactUs">
    <soap:binding transport="http://schemas.xmlsoap.org/soap/http" />
    <operation name="getContactUs">
      <soap:operation soapAction="http://nrcme.fmcsa.dot.gov/ContactUs/getContactUs" />
      <input>
        <soap:body use="literal" />
      </input>
      <output>
        <soap:body use="literal" />
      </output>
    </operation>
  </binding>
</definitions>

Thanks in advance, this forum has been great.

在此先感谢,这个论坛很棒。

采纳答案by Kris Babic

One of the problems that I see is that the WSDL is making things a little confusing. It defines both the XML data object element name and Web Service operation as 'getContactUs'. I would discuss changing the XML data element from 'getContactUs' to something similar to 'ContactInfo'. If this element changes, you will most likely see that your confusion was with this naming.

我看到的问题之一是 WSDL 使事情变得有点混乱。它将 XML 数据对象元素名称和 Web 服务操作都定义为“getContactUs”。我将讨论将 XML 数据元素从“getContactUs”更改为类似于“ContactInfo”的内容。如果此元素发生更改,您很可能会发现您对此命名感到困惑。

Typically you would have a few operations that you would need to perform to execute the web service. The first is to retrieve the WebReference proxy instance for the service. The second is to initialize any input parameters as needed. Finally you would use the proxy class to invoke the service method. I believe you have performed the second step, but you are missing the first step which would give you access to the web service invocation. The service invocation may look something similar to the following depending on you code:

通常,您需要执行一些操作来执行 Web 服务。第一个是检索服务的 WebReference 代理实例。第二个是根据需要初始化任何输入参数。最后,您将使用代理类来调用服务方法。我相信您已经执行了第二步,但是您错过了让您访问 Web 服务调用的第一步。根据您的代码,服务调用可能类似于以下内容:

WebReference.ContactUs myService = new WebReference.ContactUs();
...
<result data type> _Response = myService.getContactUs(myContactUs);
...

回答by kernel32

I've had this problem too. It's caused by .NET's DateTimetype being different than the Java-defined web service. Just use string to replace the DateTimewith a confirmed datetime format.

我也遇到过这个问题。这是由 .NET 的DateTime类型与 Java 定义的 Web 服务不同造成的。只需使用字符串将 替换DateTime为确认的日期时间格式。

回答by Justin Dearing

It seems that you are calling the service method when you call new WebReference.getContactUs();. If you check the web server logs of the machine running the java service you can verify this, However soap "functions" are defined by the element in that wsdl.

当你调用new WebReference.getContactUs();时,似乎是在调用 service 方法. 如果您检查运行 java 服务的机器的 Web 服务器日志,您可以验证这一点,但是soap“功能”是由该 wsdl 中的元素定义的。

回答by Thorbj?rn Ravn Andersen

The most important point is to have the correct WSDL for the code you need to call. THis appears to be in order.

最重要的一点是为您需要调用的代码拥有正确的 WSDL。这似乎是有序的。

I would suggest you start with Storm (http://storm.codeplex.com/) to ensure that the WSDL is correct and do initial testing of the web service. Then I have been told that Visual Studio can import a WSDL and generate client code you can then just invoke, which I would do next.

我建议您从 Storm (http://storm.codeplex.com/) 开始,以确保 WSDL 是正确的并对 Web 服务进行初始测试。然后我被告知 Visual Studio 可以导入一个 WSDL 并生成您可以调用的客户端代码,我接下来会这样做。