几个简单的小例子,其中为.net的服务器对c#客户端对java客户端,java服务器对c#客户端对java客户端。
一 。其中。net服务器书写如下
1。新建科。net的项目名字为webservice
2. 添加新建项,web服务。名字为webservice1
3.代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
namespace webservice
{
/// <summary>
/// WebService1 的摘要说明
/// </summary>
[WebService(Namespace = "http://192.168.132.16/webservice/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
// public string HelloWorld()
// {
// return "Hello World";
// }
public string show(string yourname)
{
return "http://www.ourfly.com" + "欢迎" + yourname;
}
}
}
就一个show()的方法,以便一个参数。
部署到iis下http://localhost/webservice/WebService1.asmx
二 。java服务端
1。新建一个web项目名字testWebService
2。新建接口IMyWebService.java和类MyWebService.java在com.yenange.service下
3。package com.yenange.service;
public interface IMyWebService {
public String HelloWorld(String name);
}
4。package com.yenange.service;
public class MyWebServiceImpl implements IMyWebService{
public String HelloWorld(String name)
{
return name+",你好!";
}
}
三。c#客户端
form1.designer.cs
namespace Client
{
partial class Form1
{
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(144, 135);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(75, 23);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(94, 45);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(100, 21);
this.textBox1.TabIndex = 1;
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(94, 72);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(100, 21);
this.textBox2.TabIndex = 2;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(292, 266);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private webservice.WebService1 Client;
private System.Windows.Forms.TextBox textBox2;
}
}
form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Web.Services;
using webservice;
namespace Client
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Client = new WebService1();
string name;
name = Client.show("龙卷风");//.net服务器
textBox1.Text = name;
jdeps.testWebService t = new Client.jdeps.testWebService();//java服务器
textBox2.Text = t.HelloWorld("11");
}
}
}
其中对于.net服务器要添加引用.net服务端生成的webservice.dll。对于java服务端要添加web引用服务端的wsdl。http://jdeps:9090/testWebService/services/testWebService?wsdl
四。java客户端
新建javaprojacet项目
把服务端的接口拷贝进来
com.yenange.service下的IMyWebService.java
在此文件夹下新建TestWS.java
代码如下:
package com.yenange.service;
import java.net.MalformedURLException;
import org.codehaus.xfire.XFireFactory;
import org.codehaus.xfire.client.XFireProxyFactory;
import org.codehaus.xfire.service.Service;
import org.codehaus.xfire.service.binding.ObjectServiceFactory;
import org.codehaus.xfire.client.Client;
import java.net.URL;
public class TestWS {
public static void main(String[] args)
{
//1.取得WebService的路径
String url="http://localhost:9090/testWebService/services/testWebService";
//2.创建服务
Service service=new ObjectServiceFactory().create(IMyWebService.class);
//3.创建服务代理
XFireProxyFactory factory=new XFireProxyFactory(XFireFactory.newInstance().getXFire());
//4.调用外部的WebService, 建立对象. 再测试其方法
try
{
IMyWebService obj=(IMyWebService)factory.create(service,url);
System.out.println(obj.HelloWorld("leaf"));
} catch (MalformedURLException e)
{
e.printStackTrace();
}//以上为java的
//Client c1=new Client(new URL("http://localhost/webservice/WebService1.asmx?WSDL"));
//Object[] o1=c1.invoke("HelloWorld",new String[]{});
try{
Client client=new Client(new URL("http://localhost/webservice/WebService1.asmx?WSDL")); //wsdl地址
Object[] results = client
.invoke("show", new String[] { "Firends" });
System.out.println(results[0]);
}
catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}//以上为.net的
}
}
五。测试结果
leaf,你好!//。net的
http://www.ourfly.com欢迎Firends //java的