Click here to Skip to main content
Click here to Skip to main content
Go to top

ASP.NET Webservices in Java via kSOAP

, 5 Jan 2014
Rate this:
Please Sign up or sign in to vote.
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:

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:

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


ASP.NET Webmethod in 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#

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

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:

 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.

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

Or even specify the namespace and the soap action:

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

Full Implementation

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)

Share

About the Author

Juan Carlos Recio Abad
Software Developer
Spain Spain
I´m a Computer Engineer from Spain with a lot of interest and passion about the world of technologies, I love all kind of them, as well as I do it for the science.
I like to create many sorts of utilities and tools, inventing solutions and methods to get solving the challenges which can appear.
 
I also love math and Physics, its misteries and incredible solutions, so I've always been automatizing algorithms and processes related to them on my own, whether for fun or another affairs.
 
Of course, I enjoy reading history, literature, music, and play sport!

Comments and Discussions

 
QuestionI detect some errors Pinmemberpfernandes4-Jan-14 10:17 
AnswerRe: I detect some errors PinmemberJuan Carlos Recio Abad4-Jan-14 10:56 
SuggestionLooks Good! PinmemberPaul Conrad3-Jan-14 9:16 
GeneralRe: Looks Good! PinmemberJuan Carlos Recio Abad3-Jan-14 9:59 
GeneralRe: Looks Good! PinmemberMohsin Azam4-Jan-14 4:56 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140916.1 | Last Updated 5 Jan 2014
Article Copyright 2014 by Juan Carlos Recio Abad
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid