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

apache|client

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. SummaryIn 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.

DetailsOk 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.  ResourcesApache 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

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索c#
, apache
, service
, soap
, to
The
a c 排列组合、a cup、c、数学a c、a c 概率,以便于您获取更多的相关知识。

时间: 2024-12-03 17:39:03

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

Win32下使用AJP整合Tomcat 4.0.4和Apache 1.3.26

apache 预备工作: 下载:Apache 1.2.26 http://www.apache.org/dist/httpd/binaries/win32/apache_1.3.26-win32-x86-no_src.exeTomcat 4.0.4 http://jakarta.apache.org/builds/jakarta-tomcat-4.0/release/v4.0.4/bin/jakarta-tomcat-4.0.4-LE-jdk14.exemod_jk http://jakarta

FileZilla Client 3.26.1 发布,FTP 解决方案

FileZilla Client 3.26.1 发布了,FileZilla 是一个快速.可信赖的 FTP 客户端以及服务器端的开放源代码程序,具有多种特色.直观的接口.本次更新内容如下: 修改密码设置崩溃,站点管理器包含子目录 nix:固定保存具有多个站点特定书签的站点 文章转载自 开源中国社区 [http://www.oschina.net]

FileZilla Client 3.26.0 发布,错误修复和小修改

FileZilla Client 3.26.0 发布了,FileZilla 是一个快速.可信赖的 FTP 客户端以及服务器端的开放源代码程序,具有多种特色.直观的接口.本次更新内容如下: Bug 修复和小修改: 当更改或删除主密码时,更新传输队列中服务器项目的受保护凭证 在没有用户名设置并使用"询问"登录类型的站点显示密码输入对话框时,修复记住复选框的显示 Windows .zip 二进制文件中缺少一些图标 文章转载自 开源中国社区 [http://www.oschina.net]

HOW TO: BETA: Integrate a .NET Client with an Apache SOAP 2.2 XML Web Service

apache|client|web|xml HOW TO: BETA: Integrate a .NET Client with an Apache SOAP 2.2 XML Web Service --------------------------------------------------------------------------------The information in this article applies to: Microsoft Visual Studio .N

版mysql+apache+php in lux安装指南 写的好累得说。。

apache|mysql 刚刚看到有人贴win下安装步骤刚好今天公司网站的新服务器到手.花了一上午安装mysql+php+apache.新版的老版的还是有些不同的.现在把步骤帖一下 安装步骤:1.mysql在如下页面下载mysql的for linux rpm包http://www.mysql.com/downloads/down...3.52-1.i386.rpmhttp://www.mysql.com/downloads/down...3.52-1.i386.rpm 存至/home/tmp目录

SOAP净化有线协议(三):用脚本语言编写服务

脚本 你是一个渴望永远站在技术最前沿的Java开发者吗?软件产业风云变幻,你渴望把握Web的未来,更重要的是,如何把自己数年的Java经验发挥到极致.要寻找这些问题的答案,你不必走得太远,答案就在于SOAP. SOAP(简单对象访问协议)是一种利用XML编码数据的有线协议,它为Java的平台无关性.可移植性带来了更高层次的协同操作能力.在这个关于SOAP的系列文章的第二篇中,我介绍了Apache SOAP.作为SOAP规范的实现之一,Apache SOAP简化了SOAP应用的构造.我们曾经用Ap

Linux下安装mysql+apache+php指南

.mysql教程 在如下页面下载mysql的for linux rpm包 http://www.mysql.com/downloads/down...3.52-1.i386.rpm http://www.mysql.com/downloads/down...3.52-1.i386.rpm 存至/home/tmp目录 命令列表: cd /home/tmp rpm -ivh mysql-3.23.52-1.i386.rpm           //#安装mysql server rpm -ivh m

linux下安装apache与php;Apache+PHP+MySQL配置攻略_服务器

1.apache  在如下页面下载apache的for Linux 的源码包   http://www.apache.org/dist/httpd/;  存至/home/xx目录,xx是自建文件夹,我建了一个wj的文件夹.  命令列表:   cd /home/wj  tar -zxvf httpd-2.0.54.tar.gz mv httpd-2.0.54 apache  cd apache  ./configure --prefix=/usr/local/apache2 --enable-mo

新版mysql+apache+php Linux安装指南_php基础

刚刚看到有人贴win下安装步骤 刚好今天公司网站的新服务器到手.花了一上午安装mysql+php+apache. 新版的老版的还是有些不同的.现在把步骤帖一下 安装步骤: 1.mysql 在如下页面下载mysql的for linux rpm包 http://www.mysql.com/downloads/down...3.52-1.i386.rpm http://www.mysql.com/downloads/down...3.52-1.i386.rpm 存至/home/tmp目录 命令列表: