Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

ASP.NET Webservices in Java via kSOAP

4.71/5 (4 votes)
5 Jan 2014CPOL2 min read 20.9K  
Handy bridge between ASP.NET webservices and Java

Introduction

This tip shows a brief and handy way to consume ASP.NET webservices in Java Applications.

It can be useful in some applications like Android apps. Some time ago, I used it in a project to operate with certain .NET webservices (for example, Online-PDF Documents Generation) as well as and get some kind of data hosted in servers like SQL Server, all of this from outer applications from .NET Technologies.

Background

The main idea is that calling a webmethod between both platforms will just follow that simple syntax:

Image 1

and it will result in a simple managed code.

Calling a ASP.NET webservice in Java with this way just requires that the name we specify in Java matches the name of the webmethod.

Using the Code

How to Call

Calling ASP.NET webmethod in Java:

Java
String helloworld = MyWebservice.Query("HelloWorld");


ASP.NET Webmethod in C#

C#
[WebMethod]
public string HelloWorld()
{
    return "Hello World";
}

Parameters Flexibility

Of course, any bunch of parameters can be conveyed as well, so on the method can be used overloaded:

ASP.NET Webmethod in C#

C#
[WebMethod]
public string HelloPerson(string person)
{
    return "Hello " + person;          
}

While in Java, to include parameters to the request, we use the Parameter (implemented below) class:

Java
String response = 
    MyWebservice.Query(
        "HelloPerson", 
        new Parameter("person", "John", "person".getClass())); 

As you can see, the parameter name matches with the parameter name of the webmethod.

By default, the parameter type is String.

Of course, if there are several parameters, we also can use an array:

Java
Parameter[] parameters = new Parameter[2];
Parameters.Add(new Parameter("person", "John"));
Parameters.Add(new Parameter("city", "Madrid"));
String response = MyWebservice.Query("HelloPersonInCity", parameters);

ASP.NET webmethods can return plenty of types, all of them can be serialized in Java too, that´s the reason an Object type is always returned in Java Query Method. That leads us to our decision how to serialize them, one way might be JSON.

Referencing Webservices

We have to instantiate the AspNetWebservice class (implemented below) to specify the server where we'll realize method calls.

Java
AspNetWebservice MyWebservice = new AspNetWebservice("localhost/MyWebService.asmx");  

Or even specify the namespace and the soap action:

Java
AspNetWebservice MyWebservice = new AspNetWebservice(
        "http://tempuri.org/",
        "http://tempuri.org/",
        "localhost/MyWebService.asmx");

Full Implementation

Java
public class AspNetWebservice {
        public String SOAP_NAMESPACE;
        public String WSDL_TARGET_NAMESPACE;
        public String SOAP_ADDRESS;

        public static String DEFAULT_NAMESPACE = "http://tempuri.org/"; 
        public static String DEFAULT_ACTION_NAMESPACE = "http://tempuri.org/";

    public AspNetWebservice(String soapaddress){
    	this(DEFAULT_ACTION_NAMESPACE, DEFAULT_NAMESPACE, soapaddress);
    }   
    public AspNetWebservice(String sopaaction, String namespace, String soapaddress){
        SOAP_NAMESPACE= sopaaction;
        WSDL_TARGET_NAMESPACE = namespace;
        SOAP_ADDRESS = soapaddress;
    }
    public Object Query(string WebmethodName){
        return Query(WebmethodName, null);
    }
    public Object Query(String WebmethodName, Parameter[] parameters){
        SoapObject soapRequest = new SoapObject(WSDL_TARGET_NAMESPACE, WebmethodName);
        
        // The envelop request
        SoapSerializationEnvelope soapEnvelope = 
                    new SoapSerializationEnvelope(SoapEnvelope.VER11);
	
	//Enabling ASP.NET Webservices in Java
        soapEnvelope.dotNet = true;

        if(parameters != null){
            // The properties that will be passed as parameters
            PropertyInfo pi = new PropertyInfo();
            for(Parameter param: parameters){
                pi.setName(param.getName());
                pi.setValue(param.getValue());
                pi.setType(param.getType());
                // Assigning each property to the request
                soapRequest.addProperty(pi);
            }
        }

        soapEnvelope.setOutputSoapObject(soapRequest);

        HttpTransportSE httpTransport = new HttpTransportSE(SOAP_ADDRESS);
        Object response = null;
        try
        {
                     // Send the request via a customized call method
                    httpTransport.call(SOAP_NAMESPACE + WebmethodName, envelope);
                    // Getting the response
                    response = soapEnvelope.getResponse();
        }
        catch(Exception ex){
            return "It occurred an error in the request:\n" + ex.getMessage();
        }
        return response;
    }

    public class Parameter {
        private String Name;
        private String Value;
        private Object Type;
    public Parameter(String Name, String Value, Object type){
             this.Name = Name;
             this.Value = Value;
             this.Type = type;
       }
       public Parameter(String Name, String Value){
            this(Name, Value, String.Class);
       }
       public String getName(){
            return Name;
       }
       public String getValue(){
            return Value;
       }        
       public Object getType(){
            return Type;
       }
    }
}

The implementation of this is based in the use of kSoap library.

kSOAP is a SOAP web service client library for constrained Java environments such as Applets or J2ME applications (CLDC / CDC / MIDP).

References

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)