Click here to Skip to main content
14,365,424 members

How to Consume an ASP.NET webservice from Java via SOAP

Rate this:
4.37 (24 votes)
Please Sign up or sign in to vote.
4.37 (24 votes)
18 Oct 2018
Demonstrates how to call ASP.NET webservices from the client within other languages and platforms

Sample Image - WSfromJava.jpg

Introduction

This article will be useful if you need to consume ASP.NET webservices from languages or platforms without .NET support. In this example, I am consuming a webservice from a Java applet, but it would take very little work to adapt my code to run as a Java application. Although I have only tested this on Windows XP, the code should run on other platforms, providing cross-platform client-side consumption of ASP.NET webservices.

I developed this solution because I needed to add complex client-side processing to my ASP.NET application. I did not want to have to cope with all of the slightly different JavaScript implementations used by the main browsers, and this led me to consider a Java applet.

This article shows you how to use a Java applet in your webpage and get it to communicate back to ASP.NET by consuming an XML webservice on the client.

Background

There are, no doubt, many reasons why somebody might want cross-platform consumption of ASP.NET webservices, but this is my story:

I have an ASP.NET application where I needed to add client side image manipulation facilities. I looked into writing the client-side code in JavaScript which seemed like the obvious choice, but was frustrated by different implementations in the browsers, especially when trying to use image manipulation features. For example, if you want to alpha blend your image, you would have to use img.style.MozOpacity in Netscape, img.filters.alpha.opacity in Internet Explorer and there is no way to do this at all in Opera. I really didn't want to have to write special code for each browser or downgrade the functionality to the lowest common denominator.

Therefore, I decided to try writing my client-side code in Java 1.1. Note that I'm using the Java 1.1 SDK, as Internet Explorer currently does not support code developed with Java SDKs after 1.1.4 (although it seems to me that Java SDK 1.1.7 code seems to work OK in IE). A Java applet can do all of the image manipulation I needed and is supported by many browsers. Finally, I don't have to write separate code for each browser.

The next problem was, how do I get the result of the data processing done in the Java applet back into my ASP.NET application? The answer that I came up with was to create an ASP.NET webservice and consume it in the Java applet.

A Simple Example

Take a look at the following very basic webservice:

[WebMethod]
public string ConcatWithSpace(string one, string two)
{
    return one+" "+two;
}

It could be consumed very simply by making the Java applet perform a request to: webservice.asmx/ConcatWithSpace?one=FirstValue&two=SecondValue... But what if we wanted to send larger amounts of data to the webservice? How would we go about implementing a SOAP request and response in a Java applet...? There are probably many options if you are using a more recent Java SDK, but because I want to support many browsers including Internet Explorer, I'm stuck with the Java 1.1 SDK. However, this gives us a better opportunity to understand how to talk to ASP.NET web services on other languages and platforms. Therefore, I have written my own SoapRequestBuilder class in Java that makes it quite easy to consume a service and return a single string response. Take a look at this example, which consumes the webservice above:

SoapRequestBuilder s = new SoapRequestBuilder();
s.Server = "127.0.0.1"; // server ip address or name
s.MethodName = "ConcatWithSpace";
s.XmlNamespace = "http://tempuri.org/";
s.WebServicePath = "/SimpleService/Service1.asmx";
s.SoapAction = s.XmlNamespace+s.MethodName;
s.AddParameter("one", "David");
s.AddParameter("two", "Hobbs");
String response = s.sendRequest();

In this example, the response string is filled with the result from the webservice after we sent it two parameters. The full source code of the SoapRequestBuilder class is included in the source files.

That's it! It's only a very simple example and at the moment, I'm only interested in returning a single value back from the webservice, but I have used something similar with a webservice that talks to my back-end database and this allows my Java applet to store data which can then be accessed by the ASP.NET pages.

To run the example code, set up the webservice first, then copy the Java applet files to any folder on the same machine and double-click the HTML file. All of the parameters are configured inside the HTML file, so you can change the values passed to the webservice by editing the HTML; you should not have to re-compile the Java source. I have tested the example in IE 6.0, Netscape 7.1 and Opera 7.23 on Windows XP.

Notes

Some Java Virtual Machines (like the Microsoft one) only allow you to make a socket connection to the same machine that hosts the Java class files. Therefore, if you're using an applet like me, you will need to host the Java class files on the same machine where the webservice resides.

License

This article has no explicit license attached to it, but may contain usage terms in the article text or the download files themselves. If in doubt, please contact the author via the discussion board below.

A list of licenses authors might use can be found here.

Share

About the Author


Comments and Discussions

 
QuestionGetting Error Pin
M.T. Sheikh30-May-13 2:05
memberM.T. Sheikh30-May-13 2:05 
QuestionThanks for the code Pin
Arelowo Alao16-Jun-12 8:29
memberArelowo Alao16-Jun-12 8:29 
QuestionHow to modify program not to use APPLET Pin
saimay1-Mar-12 5:35
membersaimay1-Mar-12 5:35 
QuestionThanks Pin
sanya.fesak21-Sep-11 20:06
membersanya.fesak21-Sep-11 20:06 
GeneralMy vote of 3 Pin
flzgkc7-Oct-10 22:02
memberflzgkc7-Oct-10 22:02 
Generalgetting access denied: java.net.socketexception error Pin
Member 116639322-Jul-10 6:42
memberMember 116639322-Jul-10 6:42 
QuestionHow do I pass a parameter to a .NET Web Service from a Java client Pin
Member 39953809-Mar-10 21:13
memberMember 39953809-Mar-10 21:13 
AnswerRe: How do I pass a parameter to a .NET Web Service from a Java client Pin
abhi_here30-Mar-10 5:32
memberabhi_here30-Mar-10 5:32 
GeneralPlz help !!!! Pin
Manjesh Yadav9-Aug-09 22:52
memberManjesh Yadav9-Aug-09 22:52 
GeneralHELP Pin
itcraps30-May-09 22:27
memberitcraps30-May-09 22:27 
QuestionCan return DAO/DTO? Pin
cocciolo.alessio4-Apr-09 6:10
membercocciolo.alessio4-Apr-09 6:10 
GeneralConnect Applet ant .net Services using SSL Pin
tropocolo16-Feb-09 4:44
membertropocolo16-Feb-09 4:44 
Generalcontent length Pin
garyemiller23-Jan-09 9:28
membergaryemiller23-Jan-09 9:28 
Can you please explain how you came up with the 295 + X for the content length? Where does the 295 come from? When I add parameters, or change the length of the method name, the request is refused by the server due to the content length being wrong. The code you have included to count the length of the parameters and their names does not seem to take into account the length of the enclosing angle brackets for the parameters.

What part of the SOAP you are constructing is included in the length (what part is considered content and where does it begin?)

Also - println() seems to add line terminating characters (depending upon the line.separator value defined by the system) -- are these line terminators to be taken into account when calculating the content length?
GeneralException unhandled by usercode Pin
Tsubash5-Dec-08 1:22
memberTsubash5-Dec-08 1:22 
GeneralRe: Exception unhandled by usercode Pin
circle-driver5-Dec-08 9:46
membercircle-driver5-Dec-08 9:46 
GeneralSolution for many problems Pin
circle-driver22-Nov-08 4:48
membercircle-driver22-Nov-08 4:48 
GeneralRe: Solution for many problems Pin
Ferry Sumendap26-Nov-08 19:19
memberFerry Sumendap26-Nov-08 19:19 
GeneralRe: Solution for many problems Pin
circle-driver27-Nov-08 3:23
membercircle-driver27-Nov-08 3:23 
Questionhow to call .net web sevice in java client using apache axis as iam new in web services Pin
vymokesha16-Oct-08 7:12
membervymokesha16-Oct-08 7:12 
AnswerRe: how to call .net web sevice in java client using apache axis as iam new in web services Pin
vymokesha7-Nov-08 10:01
membervymokesha7-Nov-08 10:01 
Generalmy asp.ner web service in unavailable for other program Pin
azijoon1-Aug-08 7:24
memberazijoon1-Aug-08 7:24 
GeneralRe: my asp.ner web service in unavailable for other program Pin
circle-driver22-Nov-08 4:53
membercircle-driver22-Nov-08 4:53 
QuestionMethod with no parameter or x parameter other then 2 does not work? Pin
Einstein's pal16-Jul-08 4:30
memberEinstein's pal16-Jul-08 4:30 
GeneralRe: Method with no parameter or x parameter other then 2 does not work? Pin
Decusterra24-Feb-09 6:19
memberDecusterra24-Feb-09 6:19 
AnswerRe: Method with no parameter or x parameter other then 2 does not work? Pin
Topzung213-Oct-11 2:43
memberTopzung213-Oct-11 2:43 

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

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

Article
Posted 15 Jan 2004

Stats

423.9K views
6.7K downloads
61 bookmarked