WebService(XFire)开发服务器端用java实现,客户端由C#实现。

时间:2022-04-23 13:42:32
参考资料:
http://blog.csdn.net/s_ongfei/archive/2007/09/04/1771451.aspx   
http://hi.baidu.com/lcqiang521/blog/item/e3355219f1c9bbbd4aedbc8f.html

     当时业务需要在(PDA)上运行一个wince程序,实现PDA和服务器之间的数据传输。
      因为服务器端是java程序,考虑到PDA的硬件和性能问题,客户端采用wince程序来实现数据通信。
      下面是自己写的demo.不对之处,请大家多多包涵。希望对大伙有用。
      服务器端大家根据我上面提供的2个网址里的资料,来创建。

//服务器端代码(JAVA)   
package test;   
import java.io.Serializable;   
public class Course implements Serializable {   
  
    private static final long serialVersionUID = 1L;   
       
    public String course;   
  
    public String getCourse() {   
        return course;   
    }   
  
    public void setCourse(String course) {   
        this.course = course;   
    }   
}   
  
package test;   
import java.io.Serializable;   
import java.util.ArrayList;   
import java.util.List;   
  
public class HelloServiceImpl implements Serializable,IHelloService {   
  
    public HelloServiceImpl(){   
           
    }   
       
    public Course choose(User u) {
         Course course = new Course();
	if(u==null){
		course.setCourse("查无此人!");
		return course;
	}
	if(u.getUserId().equals("001")&&u.getUserName().equals("张三")){
		course.setCourse(" 张三 的 ID 为 :001 ");
	}
		return course;
	}

	public String sayHello(String ttt) {
	     return "webservice 测试 成功!";
	}

  
    public List test(List t) {   
        List a = new ArrayList();   
        Course course = new Course();   
        course.setCourse("分数:98 ");   
        a.add(course);   
        return a;   
    }   
}   
package test;   
  
import java.util.List;   
  
public interface IHelloService {   
       
    public String sayHello(String ttt);      
  
    public Course choose(User u);   
  
    public List test(List t);   
}   
package test;   
  
import java.io.Serializable;   
  
public class User implements Serializable {   
    private static final long serialVersionUID = 1L;   
    public String userId ;   
    public String userName;   
    public String getUserId() {   
        return userId;   
    }   
    public void setUserId(String userId) {   
        this.userId = userId;   
    }   
    public String getUserName() {   
        return userName;   
    }   
    public void setUserName(String userName) {   
        this.userName = userName;   
    }   
}   
//放在src下的test包下:   
IHelloService.aegis.xml   
<?xml version="1.0" encoding="UTF-8"?>   
<mappings>   
    <mapping>   
        <method name="test">   
            <parameter index="0" componentType="java.lang.String" />   
            <return-type componentType="test.Course" />   
        </method>   
    </mapping>   
</mappings>   
  
//放在/WEB-INF/web.xml   
<?xml version="1.0" encoding="UTF-8"?>   
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">   
  <servlet>   
    <servlet-name>XFireServlet</servlet-name>   
    <servlet-class>org.codehaus.xfire.transport.http.XFireConfigurableServlet</servlet-class>   
    <load-on-startup>0</load-on-startup>   
  </servlet>   
  <servlet-mapping>   
    <servlet-name>XFireServlet</servlet-name>   
    <url-pattern>/services/*</url-pattern>   
  </servlet-mapping>   
  <welcome-file-list>   
    <welcome-file>index.jsp</welcome-file>   
  </welcome-file-list>   
</web-app>   
  
services.xml在项目中和src同级WebServices文件夹中   
  
<?xml version="1.0" encoding="UTF-8"?>   
<beans xmlns="http://xfire.codehaus.org/config/1.0">   
    <service>   
        <name>HelloService</name>   
        <namespace>http://test/HelloService</namespace>   
        <serviceClass>test.IHelloService</serviceClass>   
        <implementationClass>test.HelloServiceImpl</implementationClass>   
    </service>   
</beans>   
 
//将服务器端部署到tomcat中,开启服务器

(c#)客户端代码
往Form中拖入2个textBox.
然后项目-->添加Web引用-->地址栏输入:
http://localhost:8080/WebService/services/HelloService?wsdl
点 -->前往 
下面填Web引用名:我的就填的localhost
然后点-- 添加Web引用
--成功
代码部分.
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace TestWebservice
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
             string a ="";
            localhost.Course course = new WebServiseTest.localhost.Course();
            localhost.User user = new WebServiseTest.localhost.User();
            localhost.HelloService myClient = new WebServiseTest.localhost.HelloService();
            try
            {
                user.userId = "001";
                user.userName = "张三";
                a = myClient.sayHello("zwx");
                course = myClient.choose(user);
            }
            catch {
                a = "连接出错!";
            }
            
            textBox1.Text = a;
            textBox2.Text = course.course;
        }
    }
}

运行客户端即可。  
 
源码如下看附件,
java项目中的jar包,您根据上面网址中资料,新建个项目拷贝过来就行。