首页 > 开发 > 综合 > 正文

A C# SOAP Apache Client(4.26的好文章。Good)

2024-07-21 02:21:44
字体:
来源:转载
供稿:网友
菜鸟学堂:
introductionfirst of all, let me tell you that 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 my clients an option when 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 for writing this article is that fact that i could not find anything else like it on the web. there were plenty of articles telling me how i can use the apache soap api to use a .nettm service, but not the other way round.
we'll start with a refresher on some of the terminology i will be using:
  • soap:- simple object access protocol (soap) is a way for a program running in one kind of operating system (such as windows 2000) to communicate with a program in the same or another kind of an 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 information 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 combine 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 note that this document does not covering installing apache tomcat, apache soap or the .nettm sdk. see the resources section for more information and links to all these projects.

summary

in order to make use of a apache soap web service using c# you need to do the following steps:
  1. create a proxy class that implements all the methods you want to be able to call from your client. this class needs to extend the system.web.services.protocols.soaphttpclientprotocol
  2. the constructor of this class needs to set the url property of the above class. basically this will be the url of the apache soap rpcrouter servlet e.g. http://localhost:8080/apache-soap/servlet/rpcrouter
  3. the proxy class needs to have a webservicebindingattribute set. this defines the name of the web service and the namespace that 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.
  4. you will need to define a method in the proxy for each method that the service supports and the client wants to use. this method will need to match the signature of the method in the web service itself i.e. if your service defines a method called deleteitem which takes a string as an argument, you will need to define a method in the proxy class called deleteitem(string id).
  5. each method will need to have an associated soapdocumentmethodattribute, this defines the soapaction, as well as the name of the apache soap service that you are connecting to. it also defines the xml encoding style and how the parameters are formatted in the body of the soap request.
  6. each method will then make use of the invoke method provided by the soaphttpclientprotocol class to actually call the method on the web service and get the result.
  7. your client class will then simply need to create an instance of the proxy you have just created, and make calls on the method that it implements, the proxy handles sending the requests to the apache soap server and retrieving the results.

details

ok let's get down to business, the above list gave you a brief overview of the process, i am now going to expand on it and show you the code that i used to talk to my service.

the service

i'll start by giving you the 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 that 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 my 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 will 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         * encode the data it sends to the url as well as which         * service on the server the request is for.         *         * don't worry all is explain later in the article         */        [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 the result is a string, so we can             * safely cast it to the correct type             * we also know we are only expecting a singe object             * to be returned so only return the 1st element             */            return ((string)(results[0]));        }    }}//end of helloservice namespace
looks pretty simple doesn't it. the only thing that really needs to be explained are the sections of the code in '[' brackets. basically these are attributes associated with the method/class that provide extra information about the method/class.
the webservicebindingattribute declares that methods within this class will bind to a xml service. the methods that bind to an xml service need to define how they intend to send and receive soap messages, this is done via the soapdocumentmethodattribute. this attribute specifies the soapaction(nothing in our case), the request and response namespaces. when it comes to apache soap these namespaces actually define the service id that all calls should be directed to. the final 2 parameters define how parameters to the method are to be encoded, and how they should be 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 order for you to interact with apache soap services you will need to ensure that you always use encoded parameters. this is because apache soap does not try to guess what a parameter is, it would rather be told what it is. there are 2 format options, wrapped and bare. wrapped seems to give me the most success when working with apache soap services, apache soap complains and throws and exception 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 is done using the following command:
csc /t:library helloservice.cs
this should give us a helloservice.dll file in our 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 have to do now is compile the above class. this is done using the command:
csc helloclient.cs /r:helloservice.dll
we should now have a helloclient executable in our current directory. now if we start tomcat with apache soap installed, and ensure we have deployed our service. we should be able to run our client and get a response from the server that looks like this:
hello robert pleased to meet you.
if you are interested in seeing what is being sent across the wire, you can make use of the tcptunnelgui that comes with the apache soap package. this allows you to dump all traffic sent to a specific port, and then forward it on to the real location.
in order to see what is going on, adjust the url parameter you 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 that tomcat is running on your local machine on port 8080, and that you want the tunnel to listen on port 8070. if you run the client again you should see the soap request being sent, and the associated responses from the apache soap 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
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表