Kristin.Y Kristin.Y - 1 year ago 151
Android Question

Android as web service client site for Application Authentication with JAX-WS

So, I came across this website to learn about Application Authentication with JAX-WS. I am trying to apply it in android where android acts as the web service client.

In the example provided, service function is used to access the url.

Service service = Service.create(url, qname);

While the below code is used to pass username and password:

Map<String, Object> req_ctx = ((BindingProvider)hello).getRequestContext();
req_ctx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, WS_URL);

Map<String, List<String>> headers = new HashMap<String, List<String>>();
headers.put("Username", Collections.singletonList("mkyong"));
headers.put("Password", Collections.singletonList("password"));
req_ctx.put(MessageContext.HTTP_REQUEST_HEADERS, headers);

My question is, since android does not support, then how should I approach Application Authentication with JAX-WS using android?
Appreciate the advice. Thanks in advance

Answer Source

Yes, finally done it! I am gonna post my code here to help lost sheep like me.

import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import java.util.ArrayList;
import java.util.List;

import org.ksoap2.HeaderProperty;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;

public class SampleCode extends AppCompatActivity {

    private final String NAMESPACE = "";
    private final String SOAP_ACTION = "\"\"";
    private final String METHOD_NAME = "getHelloWorldAsString";
    private final String URL_App = "http://localhost:8080/JAX-WS-Application-Authentication-Example/HelloWorldImplService?WSDL";

    String TAG = "Repsonse";
    String response = "";

    protected void onCreate(Bundle savedInstanceState) {

        Button btnClick = (Button) findViewById(;
        btnClick.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                new AsyncCallWS().execute();

    private class AsyncCallWS extends AsyncTask<Void, Void, Void> {

        protected Void doInBackground(Void... params) {
            return null;

    private void sendHeader(){

        SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);

        SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
        envelope.dotNet = false;

        HttpTransportSE androidHttpTransport = new HttpTransportSE(URL_App);

            List<HeaderProperty> headerList = new ArrayList<HeaderProperty>();
            headerList.add(new HeaderProperty("Username", "mkyong"));
            headerList.add(new HeaderProperty("Password", "password"));

  , envelope, headerList);

            SoapPrimitive resultString = (SoapPrimitive) envelope.getResponse();

            Boolean status = Boolean.valueOf(resultString.toString());

            Log.i(TAG, "Result : " + resultString );

        }catch (Exception ex) {
            Log.e(TAG, "Exception: " + ex);
            response = "Exception";

Result is as shown below:

I/Repsonse: Result : Hello World JAX-WS - Valid User! 

I get the idea from

P/S: I am using envelope.dotNet = false because my webService does not run on Microsoft's .NET framework

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download