XmlRpc with C#/Java【转】
最近看了几个项目都是用xmlrpc协作完成的,就做了几个测试客户端和服务器端和大家一起分享。希望能对入门的同学有帮助
关于xmlrpc的介绍和规范参考http://www.xml-rpc.net/ 下面我就直奔主题举几个例子了
c#服务端
首先在VS中添加引用CookComputing.XmlRpc.dll
功能:仅仅返回一个拼接后的字符串
using System;
using CookComputing.XmlRpc;
namespace xmlrpcServerTest
{
public class server : XmlRpcService
{
[XmlRpcMethod("server.hello")] //即可写方法注解,也可写类注解,此为方法注解
public string hello(string param)
{
return "hello world "+param;
}
}
}
在配置文件中加入如下配置(归属到<system.web>节点)
<httpHandlers>
<add verb="*" path="server.aspx" type="xmlrpcServerTest.server, xmlrpcServerTest" />
</httpHandlers> 实现 web调用到应用程序类的映射
c#客户端
using System;
using CookComputing.XmlRpc;
namespace xmlrpcClientTest
{
/// <summary>
/// Summary description for Class1.
/// </summary>
public interface client
{
[XmlRpcMethod("server.hello")]
string hello(string param);
}
class xmlrpcClientTest
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: Add code to start application here
//
client iclient;
XmlRpcClientProtocol protocol;
iclient = (client)XmlRpcProxyGen.Create(typeof(client));
protocol = (XmlRpcClientProtocol)iclient;
protocol.Url = "http://localhost/xmlrpcServerTest/server.aspx";
protocol.KeepAlive = false;
string ret = iclient.hello("test"); //调用
Console.WriteLine(ret);
Console.ReadLine();
}
}
}
其中客户端的web方法名称即[ ]中的名称必须和服务端相同,否则会抛异常。
再看java:
java服务端:
首先在你所使用的IDE中导入xmlrpc组件的包
rpc代码如下、web处理部分可以用servlet或jsp来调用这个类、封装到一个方法中
//传入request和response内置对象
.........
public class server {
public void invoke() {
XmlRpcServer xmlrpc = new XmlRpcServer();
xmlrpc.addHandler("server", new serverImpl());
byte[] result = xmlrpc.execute(request.getInputStream());
..........
}
}
..........
public class serverImpl{
String function(String paramHead,String paramTail){
return paramHead+paramTail;
}
}
serverImpl为一个普通的java类,可以用来处理业务逻辑,“server
java客户端:
try{
XmlRpcClient client = new XmlRpcClient(http://localhost:8080/project/servProvider);
//project是你的工程名字
//servProvider可以是servlet或jsp
String[] param = {"hello ","world"};
Vector param_vector = new Vector();
param_vector.addElement(param[0]);
param_vector.addElement(param[1]);
String res ="sdf";
res = (String)client.execute("server.function",param_vector);
System.out.println(res);
}
catch(MalformedURLException e)
{
System.out.println(e.toString());
}
catch (IOException e) {
System.out.println(e.toString());
}
catch (XmlRpcException e) {
System.out.println(e.toString());
}
完毕,这就是很有用的xmlrpc
”可以理解为serverImpl的一个代理或标号,便于服务器端定向
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/djseaside/archive/2008/08/20/2803182.aspx
XmlRpc with C#/Java【转】的更多相关文章
- XML-RPC笔记
1.什么是XML-RPC RPC(Remote Procedure Call)就是相当于提供了一种"远程接口"来供外部系统调用,常用于不同平台.不同架构的系统之间互相调用. XML ...
- (转载)Linux 套接字编程中的 5 个隐患
在 4.2 BSD UNIX® 操作系统中首次引入,Sockets API 现在是任何操作系统的标准特性.事实上,很难找到一种不支持 Sockets API 的现代语言.该 API 相当简单,但新的开 ...
- 转 MetaWeblog API 编写
如今,许多人都熟悉个人和公司或业界主办的博客.后者明显成为了传统公司和行业网站的下一代新兴产物.博客的内容涉及从简洁的特制产品公告和公共关系到实用且深刻的主题探索,这些主题可能对公司的产品或行业的未来 ...
- JMeter实战(一) 体系结构
此为开篇,介绍JMeter的组成结构,阅读后对JMeter形成整体认知和初步印象. 为了便于后续讲解,先明确下2个术语. 元件:如HTTP请求.事务控制器.响应断言,就是一个元件. 组件:如逻辑控制器 ...
- 12张图打开JMeter体系结构全局视角
JMeter是Java技术栈工具,在软件测试领域应用非常广泛,无论是性能测试还是接口测试,技术都很成熟和稳定.它有一个突出特点:开源,适合做二次开发,以阿里为代表的Java技术栈公司都对它青睐有加.在 ...
- Spark案例分析
一.需求:计算网页访问量前三名 import org.apache.spark.rdd.RDD import org.apache.spark.{SparkConf, SparkContext} /* ...
- bugzilla4的xmlrpc接口api调用实现分享: xmlrpc + https + cookies + httpclient +bugzilla + java实现加密通信下的xmlrpc接口调用并解决登陆保持会话功能
xmlrpc . https . cookies . httpclient.bugzilla . java实现加密通信下的xmlrpc接口调用并解决登陆保持会话功能,网上针对bugzilla的实现很 ...
- java实现xml-rpc客户端和服务端
客户端代码: package test_xmlrpc.test; import java.net.URL;import java.util.ArrayList;import java.util.Lis ...
- java XML-RPC
1.XML-RPC简介 xml rpc是使用http协议做为传输协议的rpc机制,使用xml文本的方式传输命令和数据.一个rpc系统,必然包括2个部分:1.rpc client,用来向rpc serv ...
随机推荐
- js刷新页面代码
第一种: location.reload() 第二种: location.replace(location.href) 第三种: history.go() 第四种: location=location ...
- LeetCode Binary Tree PostorderTranversal
Problem Description Given a binary tree, return the postorder traversal of its nodes' values. For ex ...
- 一个前端资源站 http://www.bootcss.com/
http://www.bootcss.com/ 里面有w3schools原版的镜像网站地址: https://www.quanzhanketang.com/ 这个网站正常版本被墙了,可以通过这个镜像访 ...
- http之post方法 提交数据的四种方法
http协议中,post方法用来向服务端提交数据, 这里介绍四种方式: application/x-www-form-urlencoded multipart/form-data applicatio ...
- [水煮 ASP.NET Web API2 方法论](1-6)Model Validation
问题 想要 ASP.NET Web API 执行模型验证,同时可以和 ASP.NET MVC 共享一些验证逻辑. 解决方案 ASP.NET Web API 与 ASP.NET MVC 支持一样的验证机 ...
- cordova plugin 大全及安装命令
前言:用cordova开发hybrid app的过程中,由于是html5开发的app,在手机上很多权限受限制,这就导致了我们需要安装很多插件来弥补这一缺点.在网上搜了好久,感觉都不是很全,所以自己整理 ...
- BotFramework Nodejs示例
关于Bot Framework知识,可以参考<Nodejs Bot学习> 本文是根据bot framework sample<https://github.com/Microsoft ...
- shell脚本学习(三)
shell echo命令 显示普通字符串: echo "I am cat_crazy.'" 注:这里的双引号可以省略 显示转义字符: 如果要输出引号,*号等需要转义才能输出,如下 ...
- (7) go 函数
1.格式 调用 2.包 (1)包 本质 文件夹.每一个文件都必须属于一个包 (2)给包取别名 (3)函数的首字母大小,决定是否能被外包访问 (3) 3.多返回值 4.递归 5.基本数据类型和数组都是拷 ...
- CodeForces 144B Meeting
暴力. 题目只要求计算边上的点就可以了,一开始没看清题意,把内部的也算进去了.内部的计算可以延迟标记一下,但这题没有必要. #include<map> #include<set> ...