谁能举个大的案例,是利用Web service实现!
想看一个大的案例,体会一下Webservice 到底牛B在何处?
Web service可以连接不同的应用平台.但要是把现有的各应用包装成Web service同样需要巨大的代价?
[解决办法]
文件 study.asmx.cs
using system;
using system.collections;
using system.componentmodel;
using system.data;
using system.diagnostics;
using system.web;
using system.web.services;
using system.xml.serialization ;
namespace studyxml
{
/// <summary>
/// <br> 一个webservice的例子 </br>
/// <br> author:bigeagle@163.net </br>
/// <br> date: 2001/12/21 </br>
/// <br> history: 2001//12/21完成 </br>
/// </summary>
/// <remarks>
/// 这个webservice实现的功能很简单
/// 主要功能有两个,一个是取得预定义的item数组
/// 另一个是保存record类型的纪录
/// </remarks>
public class study : system.web.services.webservice
{
private arraylist m_arritems ;
private arraylist m_arrreocrds ;
public study()
{
//codegen: this call is required by the asp.net web services designer
initializecomponent();
this.m_arrreocrds = new arraylist() ;
this.m_arritems = new arraylist() ;
//增加一些实验数据
for(int i = 0 ; i < 100 ; i ++)
{
this.m_arritems.add(new item( "itemname " + i.tostring()
, "itemvalue " + (i + 1).tostring())) ;
}
}
/// <summary>
///
/// </summary>
/// <param name= "a_stritemname "> item name </param>
/// <returns> item对象 </returns>
private item getitem(string a_stritemname)
{
//throw(new exception(server.urldecode(a_stritemname))) ;
for(int i = 0 ; i < this.m_arritems.count ; i ++)
{
item item = (item)this.m_arritems[i] ;
if(item.name == server.urldecode(a_stritemname).trim())
{
return item ;
}
}
return null ;
}
#region component designer generated code
//required by the web services designer
private icontainer components = null;
/// <summary>
/// required method for designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void initializecomponent()
{
}
/// <summary>
/// clean up any resources being used.
/// </summary>
protected override void dispose( bool disposing )
{
if(disposing && components != null)
{
components.dispose();
}
base.dispose(disposing);
}
#endregion
[webmethod]
public void additem(string a_strname , string a_strvalue)
{
this.m_arritems.add(new item(a_strname , a_strvalue));
}
/// <summary>
/// 取得item列表
/// </summary>
/// <returns> arraylist </returns>
[webmethod]
[xmlinclude(typeof(item))]
public arraylist getitems()
{
return this.m_arritems ;
}
/// <summary>
/// 保存纪录
/// </summary>
/// <param name= "a_stritemname "> </param>
/// <param name= "a_strdemoname "> </param>
/// <param name= "a_intdemoamount "> </param>
/// <returns> 如果成功返回false,否则返回false </returns>
[webmethod]
public bool saverecord(string a_stritemname
, string a_strdemoname , int a_intdemoamount)
{
try
{
item item = this.getitem(a_stritemname) ;
if(item != null)
{
this.m_arrreocrds.add(new record(this.m_arrreocrds.count + 1
, item
, new demo(a_strdemoname , a_intdemoamount))) ;
}
else
{
throw(new exception( "指定item的name错误! ")) ;
}
return true ;
}
catch(exception e)
{
throw(new exception(e.tostring())) ;
//return false ;
}
}//end method
}//end class
/// <summary>
/// 一个简单的类,用来对应象select的option这类东西
/// </summary>
public class item
{
private string m_strname ;
private string m_strvalue ;
public string name
{
get
{
return this.m_strname ;
}
set
{
this.m_strname = value ;
}
}
public string value
{
get
{
return this.m_strvalue ;
}
set
{
this.m_strvalue = value ;
}
}
public item(string a_strname , string a_strvalue)
{
this.m_strname = a_strname ;
this.m_strvalue = a_strvalue ;
}
public item()
{
this.m_strname = " " ;
this.m_strvalue = " " ;
}
}//end class
/// <summary>
/// 简单的示例用类
/// 结构很简单,三个成员变量
/// 一个int类型的编号,
/// 一个item类型,一个demo类型
/// </summary>
public class record
{
private int m_intid ;
private item m_objmyitem ;
private demo m_objmydemo ;
public record()
{
this.m_intid = 0 ;
this.m_objmydemo = new demo() ;
this.m_objmyitem = new item() ;
}
public record(int a_intid , item a_objitem , demo a_objdemo)
{
this.m_intid = a_intid ;
this.m_objmydemo = a_objdemo ;
this.m_objmyitem = a_objitem ;
}
}//end calss
/// <summary>
/// 一个简单的示例用类
/// 结构很简单,只有两个成员变量,一个name , 一个amount
/// </summary>
public class demo
{
private string m_strname ;
private int m_intamount ;
public string name
{
get
{
return this.m_strname ;
}
set
{
this.m_strname = value ;
}
}
public int amount
{
get
{
return this.m_intamount ;
}
set
{
this.m_intamount = value ;
}
}
/// <summary>
/// 构造函数
/// </summary>
public demo()
{
this.m_intamount = 0 ;
this.m_strname = " " ;
}
/// <summary>
/// 重载构造函数
/// </summary>
/// <param name= "a_strname "> </param>
/// <param name= "a_intamount "> </param>
public demo(string a_strname , int a_intamount)
{
this.m_intamount = a_intamount ;
this.m_strname = a_strname ;
}
}//end class
}//end namespace
[解决办法]
简单的webservice开发例子
axis支持三种web service的部署和开发,分别为:
1、dynamic invocation interface ( dii)
2、stubs方式
3、dynamic proxy方式
二、编写dii(dynamic invocation interface )方式web服务
1.编写服务端程序helloclient
public class helloclient
{
public string getname(string name)
{
return "hello "+name;
}
}
2、将源码拷贝到axis_home下,重命名为 helloclient.jws
3、访问连接http://localhost:8080/axis/helloclient.jws?wsdl,页面显示axis自动生成的wsdl
4、编写访问服务的客户端 testhelloclient.java
import org.apache.axis.client.call;
import org.apache.axis.client.service;
import javax.xml.namespace.qname;
import javax.xml.rpc.serviceexception;
import java.net.malformedurlexception;
import java.rmi.remoteexception;
public class sayhelloclient2
{
public static void main(string[] args)
{
try
{
string endpoint =
"http://localhost:8080/axis/helloclient.jws ";
service service = new service();
call call = null;
call = (call) service.createcall();
call.setoperationname(new qname(
"http://localhost:8080/axis/helloclient.jws ",
"getname "));
call.settargetendpointaddress
(new java.net.url(endpoint));
string ret = (string) call.invoke(new object[]
{ "zhangsan "});
system.out.println( "return value is " + ret);
}
catch (exception ex)
{
ex.printstacktrace();
}
}
}
三、编写dynamic proxy方式访问服务
1、编写部署服务端程序,同上边dii方式,本次仍使用上边部署的helloclient
2、编写代理接口
public interface helloclientinterface
extends java.rmi.remote
{
public string getname(string name)
throws java.rmi.remoteexception;
}
3、编写并执行客户端程序testhelloclient.java
import javax.xml.rpc.service;
import javax.xml.rpc.servicefactory;
import java.net.url;
import javax.xml.namespace.qname;
public class testhelloclient
{
public static void main(string[] args)
{
try
{
string wsdlurl =
"http://localhost:8080/axis/helloclient.jws?wsdl ";
string namespaceuri =
"http://localhost:8080/axis/helloclient.jws ";
string servicename = "helloclientservice ";
string portname = "helloclient ";
servicefactory servicefactory =
servicefactory.newinstance();
service afservice =
servicefactory.createservice(new url(wsdlurl),
new qname(namespaceuri, servicename));
helloclientinterface proxy = (helloclientinterface)
afservice.getport(new qname(
namespaceuri, portname),
helloclientinterface.class);
system.out.println
( "return value is "+proxy.getname( "john ") ) ;
}catch(exception ex)
{
ex.printstacktrace() ;
}
}
}
四、编写wsdd发布web服务,编写stub client访问web服务
1、编写服务端程序server,sayhello.java,编译server.sayhello.java
package server;
public class sayhello
{
public string getname(string name)
{
return "hello "+name;
}
}
2.编写loghandler.java
import org.apache.axis.axisfault;
import org.apache.axis.handler;
import org.apache.axis.messagecontext;
import org.apache.axis.handlers.basichandler;
import java.util.date;
public class loghandler
extends basichandler
{
public void invoke
(messagecontext msgcontext)
throws axisfault
{
/** log an access each time
we get invoked.
*/
try {
handler servicehandler
= msgcontext.getservice();
integer numaccesses =
(integer)servicehandler.getoption( "accesses ");
if (numaccesses == null)
numaccesses = new integer(0);
numaccesses = new integer
(numaccesses.intvalue() + 1);
date date = new date();
string result =
date + ": service " +
msgcontext.gettargetservice() +
" accessed " + numaccesses + " time(s). ";
servicehandler.setoption
( "accesses ", numaccesses);
system.out.println(result);
} catch (exception e)
{
throw axisfault.makefault(e);
}
}
}
3、编写wsdd文件
deploy.wsdd
<deployment xmlns=
"http://xml.apache.org/axis/wsdd/ "
xmlns:java=
"http://xml.apache.org/axis/wsdd/providers/java ">
<handler name= "print " type= "java:loghandler "/>
<service name= "sayhello "
provider= "java:rpc ">
<requestflow>
<handler type= "print "/>
</requestflow>
<parameter name= "classname "
value= "server.sayhello "/>
<parameter name= "allowedmethods "
value= "* "/>
</service>
</deployment>
3、将编译后的文件拷贝到axis_home/web-inf/classes下,如:d:\tomcat\webapps\axis\web-inf\classes
4、发布服务:
java org.apache.axis.client.adminclient deploy.wsdd
5、生成client stub文件
a:方式1
将sayhello.java拷贝到axis_home/下,重命名为sayhello.jws,
执行下面的命令生存client stub
java org.apache.axis.wsdl.wsdl2java
-p client http://localhost:8080
/axis/services/sayhello.jws?wsdl
b:方式2
执行如下命令生成sayhello.wsdl
java org.apache.axis.wsdl.java2wsdl
-osayhello.wsdl -lhttp://localhost:8080
/axis/services/sayhello -nsayhello server.sayhello
执行如下命令生成client stub
java org.apache.axis.wsdl.wsdl2java
sayhello.wsdl -p client
生成的stub client文件列表为:
1.sayhello.java
2.sayhelloservice.java。
3.sayhelloservicelocator.java
4.sayhellosoapbindingstub.java
6、编写客户端程序,编译并执行
public class sayhelloclient
{
public static void main(string[] args)
{
try
{
sayhelloservice service = new client.
sayhelloservicelocator();
client.sayhello_porttype
client = service.getsayhello();
string retvalue=client.getname( "zhangsan ");
system.out.println(retvalue);
}
catch (exception e)
{
system.err.println
( "execution failed. exception: " + e);
}
}
}
[解决办法]
没有注意过
[解决办法]
大的案例估计没有
小的案例很多……
WebService是基于XML的,效率太低
如果是大的案例的时候不如用Socket