Apache|client
Introduction' All ', let me tell you ' I am not ' a C # expert. I am primarily a Java developer but have been experimenting C # and the. NET platform. Basically I am developing a SOAP Web Service using Java and Apache soap, and have decided to give me clients an option whe N and how they access the service. Hence I am going to provide a web/jsp user interface and a native Windows application written in C #. My main motivation to writing this article was that fact that I could don't find anything else as it on the web. There were plenty of articles telling me how I can use the Apache SOAP APIs to use a. NET
TMService, but not the other way round.
We ll start with a refresher on some of the terminology I would be using:
- SOAP:-Simple Object Access Protocol (soap) are a way for a program running in one kind of operating system (such As Windows communicate with a program in the same or another kind of ' a operating system (such as Linux) by using The world Wide Web "s hypertext Transfer Protocol (HTTP) and its extensible Markup Language (XML) as the mechanisms for INF Ormation Exchange
- Apache SOAP:-The Apache foundations implementation of the SOAP protocol written in Java.
- C #:-C # (pronounced "c-sharp") is a new object-oriented programming language from Microsoft, which aims to combin E The computing power of C + + with the programming ease of Visual Basic. C # is based on C + + and contains features similar to those of Java.
Please covering installing Apache Tomcat, Apache SOAP or the. NET does
TMSdk. Information and links to all of these projects.
SummaryIn order to make use of a Apache SOAP Web service using C # you need the following steps:
- Create a proxy class "Implements all" methods you want to is able to call from your client. This class needs to extend the System.Web.Services.Protocols.SoapHttpClientProtocol
- The constructor of this class needs to set the URL property of the above class. Basically this would be the URL of the Apache SOAP rpcrouter servlet e.g. http://localhost:8080/apache-soap/servlet/rpcrout Er
- The proxy class needs to have a webservicebindingattribute set. This defines the name of the Web service and the "namespace" that's the service is to use. The Name is usually the ID of the Apache SOAP Service. The namespace is usually the URL of the server hosting the service.
- You'll need to define a by the proxy for each of the service supports and the client wants to use. This method would need to match the signature of the "the" in the Web service itself i.e. if your service defines a Called DeleteItem which takes a string as a argument, you'll need to define a the proxy class called DeleteI TEM (string id).
- Each method would need to have a associated soapdocumentmethodattribute, this defines the SOAPAction, as as is the name The Apache SOAP Service that you are connecting to. It also defines the XML encoding style and the parameters are formatted in the the SOAP request.
- Each method would then make with the Invoke method provided by the SoapHttpClientProtocol class to actually call the meth OD on the Web service and get the result.
- Your client class would then simply need to create a instance of the proxy you have just, and make created on the Me Thod that it implements, the proxy handles sending the requests to the Apache SOAP server and retrieving the results.
DetailsOk let's get down to business, the above list gave your a brief overview of the process, I am now going to expand on it and Show your code that I used to talk to my service.
The Service
I ' ll start by giving your code to my Apache SOAP service:
Package Com.konnect.soap;public class helloservice{public string Hello (string name) {return "Hello" +name+ " Pleased to meet you "; }}
That's it, now all you have to do it compile the class, and place it in a location so Apache SOAP can load it from, and Then deploy it using the Apache SOAP Admin tool. In this example I have given the service an ID of "Urn:helloservice".
The C # Proxy Class
Since I wrote the service I know the exact signature of all the methods in my service. So I proxy class only needs to implement 1 method. The code is below:
Namespace Helloservice{ using system.diagnostics; using System.xml.serialization; using system; using System.Web.Services; using system.web.services.protocols; [ System.Web.Services.WebServiceBindingAttribute (name= "Urn:itemmanager", namespace= "http://localhost:8070/ Apache-soap/servlet/rpcrouter ")] public class Helloproxy: system.web.services.protocols.soaphttpclientprotocol { public Helloproxy () { //you'll need to adjust this Url to reflect the location of your service . this. URL = "Http://localhost:8080/apache-soap/servlet/rpcrouter"; } /* The following attribute tells the Soap client how to & nbsp; * encode the the data it sends to the URL as as as the AS is WHICH   &NB sp; * service on the server's request is for. * * Don ' t worry all are explain later in the article& nbsp; */ [ System.Web.Services.Protocols.SoapDocumentMethodAttribute ("", requestnamespace= "Urn:helloservice"), Responsenamespace= "Urn:helloservice", use=system.web.services.description.soapbindinguse.encoded, ParameterStyle =system.web.services.protocols.soapparameterstyle.wrapped)] public string Hello (string name) { /* Call the "Invoke method" with the method * name, and its arguments as an array of objects. */ object [] results = this. Invoke ("Hello", new object[] {name}); /* we Know that's the result are a string, so we can * safely cast it to the correct type * We also know we are only expecting a singe object & Nbsp; * to be returned the 1st element & nbsp; */ return ((String) (Results[0])); } }}//end of HelloService Namespace
Looks pretty simple doesn ' t it. The only thing that really needs is explained are the sections of the code in ' [' brackets. Basically these are attributes associated and the method/class that provide extra.
The WebServiceBindingAttribute declares that methods within this class would bind to a XML service. The methods that bind to a XML service need to define how they intend to send and receive SOAP messages. The Soapdocumentmethodattribute. This attribute specifies the SOAPAction, the request and response namespaces. When it comes to Apache SOAP this namespaces actually define the service ID that all calls should is directed to. The final 2 parameters define how parameters to the method are to is encoded, and how they should is formatted in the body section of the SOAP envelope.
There are 2 main types of encoding Literal, and encoded, Literal encoding-does not provide any extra hints as to the type Of the parameter being sent across the wire. Encoded however does provide this extra information. In the order of interact with the Apache SOAP services you'll need to ensure this always use encoded parameters. This is because the Apache SOAP does not try to guess what a parameter are, it would rather be told the It is. There are 2 format options, wrapped and bare. Wrapped seems to give me the most success while working with Apache SOAP services, Apache Soap complains and throws and exc Eption when Bare parameters are sent across.
We need to complile this class as a library (DLL), and then reference it when we compile our client. This are done using the following command:
Csc/t:library HelloService.cs
This is should give us a HelloService.dll file in my current directory. ]
The Client
The final step. The listing for the client is below:
Namespace helloservice{public class Helloclient {public static void Main (String [] args) { HelloService service = new HelloService (); Console.WriteLine (Service.hello ("Robert")); }}}//end of the HelloService namespace
All we are have to did is compile the above class. This are done using the command:
CSC Helloclient.cs/r:helloservice.dll
We should now have a helloclient executable into our current directory. Now if we start Tomcat with the Apache SOAP installed, and ensure we have deployed our service. We are able to run my client and get a response from the "should" looks like this:
Hello Robert pleased to meet for you.
If you are are interested in seeing what are being sent the across, you can make use of the wire this Tcptunnelgui with T He Apache SOAP package. This is allows to dump all traffic sent to a specific port, and then forward it in to the real location.
In order to the what are going on, adjust the URL parameter your specified in the HelloService C # class have a port of 8070. Then start the Tcptunnelgui using the following command:
Java-jar <path to soap.jar> org.apache.soap.util.net.TcpTunnelGui 8070 localhost 8080
This assumes of that Tomcat was running on your local machine on port 8080, and so you want the tunnel to listen on port 807 0. If you run the client again you should to the SOAP request being sent, and the associated responses from the Apache so AP server.
Resources
Apache Tomcat:-http://jakarta.apache.org/tomcat/
Apache-soap:-http://xml.apache.org/soap/[tr]
. NETTM SDK:-msdn.microsoft.com[tr]
The C # Corner:-http://www.c-sharpcorner.com