How to Consume WCF Service with Android How to Consume WCF Service with Android android android

How to Consume WCF Service with Android


To get started with WCF, it might be easiest to just use the default SOAP format and HTTP POST (rather than GET) for the web-service bindings. The easiest HTTP binding to get working is "basicHttpBinding". Here is an example of what the ServiceContract/OperationContract might look like for your login service:

[ServiceContract(Namespace="http://mycompany.com/LoginService")]public interface ILoginService{    [OperationContract]    string Login(string username, string password);}

The implementation of the service could look like this:

public class LoginService : ILoginService{    public string Login(string username, string password)    {        // Do something with username, password to get/create sessionId        // string sessionId = "12345678";        string sessionId = OperationContext.Current.SessionId;        return sessionId;    }}

You can host this as a windows service using a ServiceHost, or you can host it in IIS like a normal ASP.NET web (service) application. There are a lot of tutorials out there for both of these.

The WCF service config might look like this:

<?xml version="1.0" encoding="utf-8" ?><configuration>    <system.serviceModel>        <behaviors>            <serviceBehaviors>                <behavior name="LoginServiceBehavior">                    <serviceMetadata />                </behavior>            </serviceBehaviors>        </behaviors>        <services>            <service name="WcfTest.LoginService"                     behaviorConfiguration="LoginServiceBehavior" >                <host>                    <baseAddresses>                        <add baseAddress="http://somesite.com:55555/LoginService/" />                    </baseAddresses>                </host>                <endpoint name="LoginService"                          address=""                          binding="basicHttpBinding"                          contract="WcfTest.ILoginService" />                <endpoint name="LoginServiceMex"                          address="mex"                          binding="mexHttpBinding"                          contract="IMetadataExchange" />            </service>        </services>    </system.serviceModel></configuration>

(The MEX stuff is optional for production, but is needed for testing with WcfTestClient.exe, and for exposing the service meta-data).

You'll have to modify your Java code to POST a SOAP message to the service. WCF can be a little picky when inter-operating with non-WCF clients, so you'll have to mess with the POST headers a little to get it to work. Once you get this running, you can then start to investigate security for the login (might need to use a different binding to get better security), or possibly using WCF REST to allow for logins with a GET rather than SOAP/POST.

Here is an example of what the HTTP POST should look like from the Java code. There is a tool called "Fiddler" that can be really useful for debugging web-services.

POST /LoginService HTTP/1.1Content-Type: text/xml; charset=utf-8SOAPAction: "http://mycompany.com/LoginService/ILoginService/Login"Host: somesite.com:55555Content-Length: 216Expect: 100-continueConnection: Keep-Alive<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><Login xmlns="http://mycompany.com/LoginService"><username>Blah</username><password>Blah2</password></Login></s:Body></s:Envelope>


Another option might be to avoid WCF all-together and just use a .NET HttpHandler. The HttpHandler can grab the query-string variables from your GET and just write back a response to the Java code.


You will need something more that a http request to interact with a WCF service UNLESS your WCF service has a REST interface. Either look for a SOAP web service API that runs on android or make your service RESTful. You will need .NET 3.5 SP1 to do WCF REST services:

http://msdn.microsoft.com/en-us/netframework/dd547388.aspx