java 通过 Android 客户端连接到 WCF 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7686659/
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 20:59:25  来源:igfitidea点击:

Connecting to a WCF Service through Android Client

javaandroidwcfweb-services

提问by Tiago

I have a very simples Hello World WCF Service, that looks like this:

我有一个非常简单的 Hello World WCF 服务,如下所示:

namespace MyWCFServices
{
    public class HelloWorldService : IHelloWorldService
    {

        public String GetMessage()
        {
            return "Hello world";
        }
}
}

namespace MyWCFServices
{
   [ServiceContract(Namespace = "http://127.0.0.1:49359/GetMessage/")]
    public interface IHelloWorldService
    {
        [OperationContract]
        String GetMessage();
    }
}

My web.config:

我的 web.config:

<?xml version="1.0"?>
<configuration>
  <system.web>
    <compilation debug="false" targetFramework="4.0" />
  </system.web>
  <system.webServer>
    <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
  <system.serviceModel>
    <behaviors>
      <serviceBehaviors>
        <behavior name="MyServiceTypeBehaviors">
          <serviceMetadata httpGetEnabled="true" />
          <serviceDebug includeExceptionDetailInFaults="false" />
        </behavior>
      </serviceBehaviors>
    </behaviors>
    <services>
      <service name="MyWCFServices.HelloWorldService"
             behaviorConfiguration="MyServiceTypeBehaviors">

        <host>
          <baseAddresses>
            <add baseAddress="http://127.0.0.1:49359/HostDevServer/HelloWorldService.svc" />
          </baseAddresses>
        </host>

        <endpoint name="GetMessage" address="" binding="basicHttpBinding"
             contract="MyWCFServices.IHelloWorldService"/>
        <endpoint contract="IMetadataExchange"
           binding="mexHttpBinding" address="mex"/>
      </service>
    </services>
  </system.serviceModel>
</configuration>

And my android java:

还有我的安卓 java:

HttpClient httpClient = new DefaultHttpClient(); 

        String url = "http://10.0.2.2:49359/HostDevServer/HelloWorldService.svc/GetMessage";
        //String url = "http://10.0.2.2:49359/HostDevServer/HelloWorldService.svc/GetMessage";
        //String url = "http://localhost:49359/HostDevServer/HelloWorldService.svc";
        //String url = "http://localhost:49359/GetMessage";
        try{

        HttpGet method = new HttpGet( new URI(url) );
        HttpResponse response = httpClient.execute(method);
        if ( response != null )
        {
            Log.i( "login", "received " + getResponse(response.getEntity()) );
        }
        else
        {
            Log.i( "login", "got a null response" );
        }

I'm getting the java.net.SocketException: Permission denied. I already have:

我收到 java.net.SocketException: Permission denied 。我已经有了:

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

So or my WS is not accepting connections or the android client is connecting in wrong way. Probably both. For those who know, in the android client its needed to place 10.0.2.2 to redirect to a localhost web server, so no problem there (at least it works when connecting to a PHP KSOAP WS locally)

所以或者我的 WS 不接受连接或者 android 客户端以错误的方式连接。可能两者都有。对于那些知道的人,在 android 客户端中需要放置 10.0.2.2 以重定向到本地主机 Web 服务器,所以没问题(至少在本地连接到 PHP KSOAP WS 时它可以工作)

Can anyone please guide me?

任何人都可以请指导我吗?

回答by Ladislav Mrnka

That is completely wrong. It looks like you don't understand basics of web services. You are exposing SOAP service (basicHttpBinding) - it expects HTTP POST requests and communication must follow SOAP 1.1 protocol. But you are calling it as HTTP GET = no SOAP body. Also namespace in your ServicContracthas nothing to do with real service's URL.

那是完全错误的。您似乎不了解 Web 服务的基础知识。您正在公开 SOAP 服务 (basicHttpBinding) - 它需要 HTTP POST 请求并且通信必须遵循 SOAP 1.1 协议。但是您将其称为 HTTP GET = no SOAP body。此外,您的命名空间ServicContract与真实服务的 URL 无关。

To call that service you must build valid SOAP request and post it. It is better to use some SOAP protocol stack on Android - for example kSoap2.

要调用该服务,您必须构建有效的 SOAP 请求并发布它。最好在 Android 上使用一些 SOAP 协议栈 - 例如kSoap2

Hereis example of using kSoap2 to call WCF service.

下面是使用 kSoap2 调用 WCF 服务的示例。

回答by Pablo Cibraro

As Lanislav mentions, you can use SOAP. However, it's always recommended for services that need to be consumed from devices to use REST Services. Those are easy to consume from any platform that ships with a http client library. WCF also provides a way to implement REST Services using the WebHttpBinding and the WebGet attribute in your case for receiving the GET http message.

正如 Lanislav 提到的,您可以使用 SOAP。但是,始终建议需要从设备消费的服务使用 REST 服务。这些很容易从任何带有 http 客户端库的平台上使用。WCF 还提供了一种使用 WebHttpBinding 和 WebGet 属性实现 REST 服务的方法,用于接收 GET http 消息。

回答by dten

If you're using SOAP you should be able to get the WSDL from http://10.0.2.2:49359/HostDevServer/HelloWorldService.svc?WSDLthen use wsdl2Java to create classes to use from it :)

如果您使用 SOAP,您应该能够从http://10.0.2.2:49359/HostDevServer/HelloWorldService.svc?WSDL获取 WSDL,然后使用 wsdl2Java 创建要使用的类:)

but rest is best ! ;)

但休息是最好的!;)