How to Consume WCF Service with Android

The question:

I am creating a server in .NET and a client application for Android. I would like to implement an authentication method which sends username and password to server and a server sends back a session string.

I’m not familiar with WCF so I would really appreciate your help.

In java I’ve written the following method:

private void Login()
  HttpClient httpClient = new DefaultHttpClient();
      String url = "";

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

private String getResponse( HttpEntity entity )
  String response = "";

    int length = ( int ) entity.getContentLength();
    StringBuffer sb = new StringBuffer( length );
    InputStreamReader isr = new InputStreamReader( entity.getContent(), "UTF-8" );
    char buff[] = new char[length];
    int cnt;
    while ( ( cnt = buff, 0, length - 1 ) ) > 0 )
      sb.append( buff, 0, cnt );

      response = sb.toString();
  } catch ( IOException ioe ) {

  return response;

But on the server side so far I haven’t figured out anything.

I would be really thankful if anyone could explain how to create an appropriate method string Login(string username, string password) with appropriate App.config settings and Interface with appropriate [OperationContract] signature in order to read these two parameters from client and reply with session string.


The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

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:

public interface ILoginService
    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" ?>

                <behavior name="LoginServiceBehavior">
                    <serviceMetadata />

            <service name="WcfTest.LoginService"
                     behaviorConfiguration="LoginServiceBehavior" >
                        <add baseAddress="" />
                <endpoint name="LoginService"
                          contract="WcfTest.ILoginService" />

                <endpoint name="LoginServiceMex"
                          contract="IMetadataExchange" />

(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.1
Content-Type: text/xml; charset=utf-8
SOAPAction: ""
Content-Length: 216
Expect: 100-continue
Connection: Keep-Alive

<s:Envelope xmlns:s="">
<Login xmlns="">

Method 2

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.

Method 3

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:

Method 4

From my recent experience i would recommend ksoap library to consume a Soap WCF Service, its actually really easy, this anddev thread migh help you out too.

Method 5

If I were doing this I would probably use WCF REST on the server and a REST library on the Java/Android client.

All methods was sourced from or, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment