将 Java 应用程序连接到 WCF Web 服务时出错

发布于 2024-10-07 22:18:18 字数 3189 浏览 8 评论 0原文

我在 Eclipse 中编写了一个简单的 Java 控制台应用程序,它引用了用 C# 编写的 WCF Web 服务。在我的 PC 上本地托管 WCF 服务,我无法使用 java 客户端连接到该服务。

我创建 WCF 服务的步骤如下:

  1. 使用以下端点创建“Service1.svc”:

    字符串 IService1.Hello(字符串 sName) { 返回“你好”+sName+“!” ; }

  2. 该服务的网络配置如下:

     ;
        <编译debug=“true”targetFramework=“4.0”/>
      
      <系统.serviceModel>
        <绑定>
          <基本HttpBinding>
            <绑定名称=“BasicHttpBinding”/>
          
        
        <客户端>
          <端点绑定=“basicHttpBinding”绑定配置=“BasicHttpBinding”
            契约=“WcfService1.IService1”名称=“BasicHttpEndpoint”/>
        
        <行为>
          <服务行为>
            <行为>
              
              
              
              
            
          
        
        
      
     <系统.web服务器>
        <模块 runAllManagedModulesForAllRequests="true"/>
    


  3. 我修改了属性,因此服务将始终使用端口 8888。

  4. 我已使用 C# 控制台应用程序测试了该服务。

    p>

为了创建 Java 客户端,我执行了以下操作:

  1. 下载并安装与 Metro(Web 服务器)捆绑在一起的 Glassfish 3.0.1(应用程序服务器)

  2. 使用 jdk 的“bin”目录中的“wsimport”工具为 Java 客户端应用程序生成服务参考代码。我运行的用于创建服务引用的 .bat 文件

  3. 将上述步骤 2. 中的代码复制到 Eclipse 中的新 Java 应用程序中。

  4. 在我的控制台应用程序中创建一个新的 Java 类,如下所示调用 Web 服务

`

import java.net.MalformedURLException;

import java.net.URL;

import javax.xml.namespace.QName;

import javax.xml.ws.BindingProvider;

import org.tempuri.Service1;

import org.tempuri.IService1;


public class Main {

/**
 * @param args
 */
public static void main(String[] args) {

    try {
        Service1 service = new Service1(new URL("http://localhost:8888/Service1.svc?wsdl"), new QName("http://tempuri.org", "Service1"));
        IService1 port = service.getBasicHttpBindingIService1();
        ((BindingProvider)port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://localhost:8888/Service1.svc");
        String sFileName = port.hello("Cal");

        System.out.println(sFileName);      


    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

        e.printStackTrace();
    }
}
}

`

当我尝试时遇到的错误运行我的应用程序如下:

Exception in thread "main" javax.xml.ws.WebServiceException: {http://tempuri.org}Service1 is not a valid service. Valid services are: {http://tempuri.org/}Service1
    at com.sun.xml.ws.client.WSServiceDelegate.(WSServiceDelegate.java:237)
    at com.sun.xml.ws.client.WSServiceDelegate.(WSServiceDelegate.java:182)
    at com.sun.xml.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:106)
    at javax.xml.ws.Service.(Unknown Source)
    at org.tempuri.Service1.(Service1.java:42)
    at Main.main(Main.java:17)
 

感谢任何对此的帮助。谢谢。

I've written a simple Java Console Application in Eclipse that references a WCF web service written in C#. Hosting the WCF service locally on my PC, I'm unable to connect to the service using the java client.

The steps I've taken to create the WCF Service are as follows

  1. Create a 'Service1.svc' with the following endpoint:

    string IService1.Hello(string sName)
    {
    return "Hello " + sName + "!" ;
    }

  2. The web config for the service is as follows:

      <system.web>
        <compilation debug="true" targetFramework="4.0" />
      </system.web>
      <system.serviceModel>
        <bindings>
          <basicHttpBinding>
            <binding name="BasicHttpBinding" />
          </basicHttpBinding>
        </bindings>
        <client>
          <endpoint binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding"
            contract="WcfService1.IService1" name="BasicHttpEndpoint" />
        </client>
        <behaviors>
          <serviceBehaviors>
            <behavior>
              <!-- To avoid disclosing metadata information, set the value below to false and remove the metadata endpoint above before deployment -->
              <serviceMetadata httpGetEnabled="true"/>
              <!-- To receive exception details in faults for debugging purposes, set the value below to true.  Set to false before deployment to avoid disclosing exception information -->
              <serviceDebug includeExceptionDetailInFaults="false"/>
            </behavior>
          </serviceBehaviors>
        </behaviors>
        <serviceHostingEnvironment multipleSiteBindingsEnabled="true" />
      </system.serviceModel>
     <system.webServer>
        <modules runAllManagedModulesForAllRequests="true"/>
    

  3. I modified the properties so the service will always use port 8888.

  4. I've tested the service using a C# console application.

To Create the Java client, I did the following:

  1. Download and install Glassfish 3.0.1 (Application server) which come bundled with Metro (Web Server)

  2. Use the 'wsimport' tool in the 'bin'directory of my jdk to generate the service reference code for the java client app. The .bat file that I ran to create the service reference

  3. Copy the code from step 2. above into a new Java Application in Eclipse.

  4. Create a new Java class in my console app which calls the web service as follows

`

import java.net.MalformedURLException;

import java.net.URL;

import javax.xml.namespace.QName;

import javax.xml.ws.BindingProvider;

import org.tempuri.Service1;

import org.tempuri.IService1;


public class Main {

/**
 * @param args
 */
public static void main(String[] args) {

    try {
        Service1 service = new Service1(new URL("http://localhost:8888/Service1.svc?wsdl"), new QName("http://tempuri.org", "Service1"));
        IService1 port = service.getBasicHttpBindingIService1();
        ((BindingProvider)port).getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://localhost:8888/Service1.svc");
        String sFileName = port.hello("Cal");

        System.out.println(sFileName);      


    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

        e.printStackTrace();
    }
}
}

`

The error I'm getting when I attempt to run my application is as follows:

Exception in thread "main" javax.xml.ws.WebServiceException: {http://tempuri.org}Service1 is not a valid service. Valid services are: {http://tempuri.org/}Service1
    at com.sun.xml.ws.client.WSServiceDelegate.(WSServiceDelegate.java:237)
    at com.sun.xml.ws.client.WSServiceDelegate.(WSServiceDelegate.java:182)
    at com.sun.xml.ws.spi.ProviderImpl.createServiceDelegate(ProviderImpl.java:106)
    at javax.xml.ws.Service.(Unknown Source)
    at org.tempuri.Service1.(Service1.java:42)
    at Main.main(Main.java:17)
 

Any assistance with this is appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心在旅行 2024-10-14 22:18:19

将:更改

new QName("http://tempuri.org", "Service1")

为:

new QName("http://tempuri.org/", "Service1")

注意 org.

change:

new QName("http://tempuri.org", "Service1")

to:

new QName("http://tempuri.org/", "Service1")

notice the extra "/" after org.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文