Consume .NET web-service in Java.

November 13, 2006

Make the necessary changes to the web-service project in the web.config file to make it accessible to the HTTP GET & POST calls.

This is a simple Java program illustrating how you can consume the .NET web-service in Java program.

import java.io.*;
import java.net.*;

public class WebService
{
public static void main(String[] args) throws Exception
{
URL webserviceURL = new URL(“http://localhost/MyServices/WebService1.asmx/Service1”);
URLConnection webserviceConnection = webserviceURL.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(webserviceConnection.getInputStream()));
String inputLine;

while ((inputLine = in.readLine()) != null)
System.out.println(inputLine);
in.close();
}

}

In response your get the proper XML data. So, now you need to parse the XML so as to get the essential data from it. As you can see that consuming your .NET web-service in Java is not a tricky thing 🙂

-Bugs!

Advertisements

One Response to “Consume .NET web-service in Java.”

  1. Anand Says:

    i tried this example.
    but its showing following error

    C:\javaprograms>javac webService.java

    C:\javaprograms>java webService
    Exception in thread “main” java.lang.UnsupportedClassVersionError: webService (U
    nsupported major.minor version 49.0)
    at java.lang.ClassLoader.defineClass0(Native Method)
    at java.lang.ClassLoader.defineClass(Unknown Source)
    at java.security.SecureClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.defineClass(Unknown Source)
    at java.net.URLClassLoader.access$100(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClassInternal(Unknown Source)

    C:\javaprograms>


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: