简体   繁体   English

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

[英]Connecting to a WCF Service through Android Client

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: 而我的android 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. 我正在获取java.net.SocketException:权限被拒绝。 I already have: 我已经有了:

<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. 因此,或者我的WS不接受连接,或者android客户端以错误的方式连接。 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) 对于那些知道的人,在android客户端中需要将10.0.2.2重定向到本地主机Web服务器,所以在那里没有问题(至少在本地连接到PHP KSOAP WS时它可以工作)

Can anyone please guide me? 有人可以指导我吗?

That is completely wrong. 那是完全错误的。 It looks like you don't understand basics of web services. 您似乎不了解Web服务的基础知识。 You are exposing SOAP service (basicHttpBinding) - it expects HTTP POST requests and communication must follow SOAP 1.1 protocol. 您正在公开SOAP服务(basicHttpBinding)-它期望HTTP POST请求和通信必须遵循SOAP 1.1协议。 But you are calling it as HTTP GET = no SOAP body. 但是您将其称为HTTP GET = no SOAP body。 Also namespace in your ServicContract has nothing to do with real service's URL. 而且ServicContract名称空间与真实服务的URL无关。

To call that service you must build valid SOAP request and post it. 要调用该服务,您必须构建有效的SOAP请求并将其发布。 It is better to use some SOAP protocol stack on Android - for example kSoap2 . 最好在Android上使用一些SOAP协议栈,例如kSoap2

Here is example of using kSoap2 to call WCF service. 是使用kSoap2调用WCF服务的示例。

As Lanislav mentions, you can use SOAP. 正如Lanislav所提到的,您可以使用SOAP。 However, it's always recommended for services that need to be consumed from devices to use REST Services. 但是,始终建议为需要使用设备才能使用REST服务的服务提供建议。 Those are easy to consume from any platform that ships with a http client library. 这些很容易从带有http客户端库的任何平台上使用。 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. WCF还提供了一种在您的情况下使用WebHttpBinding和WebGet属性来实现REST服务的方式,以接收GET http消息。

If you're using SOAP you should be able to get the WSDL from http://10.0.2.2:49359/HostDevServer/HelloWorldService.svc?WSDL then 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 ! 但是休息是最好的! ;) ;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM