Connecting to a WCF Service through Android Client

12,788

Solution 1

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 ServicContract has nothing to do with real service's 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.

Here is example of using kSoap2 to call WCF service.

Solution 2

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.

Solution 3

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 :)

but rest is best ! ;)

Share:
12,788
Tiago
Author by

Tiago

Updated on June 04, 2022

Comments

  • Tiago
    Tiago almost 2 years

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

    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:

    <?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:

    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:

    <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)

    Can anyone please guide me?

  • Tiago
    Tiago over 12 years
    Yes, I know how to handle KSOAP (as I said I already have an app connecting to a Soap WS (php)). I was trying to follow a more "direct" approach, but I realize now that does not exist. Meanwhile I found that I have to use Soap (instead Rest or anything other) However in the WS (WCF) side, I have no idea what to do. Can you please refer a example?
  • Tiago
    Tiago over 12 years
    I agree with you, but unfortunately I have to use Soap :)