在上一回中我们创建了一个WebService服务(WebService开发笔记 1 -- 利用cxf开发WebService竟然如此简单
),下面就来作一个跨平台访问WebServcie服务的例子....
下面将在vs2005中通过c#.net访问我们创建好的WebService服务,C#.net第一次用,TNN的没想到这么简单,MS就是MS,不服不行。
1.首先在vs中新建项目
2.添加web引用....
3.输入WebService地址:http://88.148.29.54:8080/aio/services/WebServiceSample?wsdl最后点击“添加引用”
4.创建一个简单的form
5.编写按钮事件代码
using System;using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Net;
using System.Windows.Forms;
using WindowsApplication1.WebReference;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
WindowsApplication1.WebReference.WebServiceSampleImplService service = null;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
String str = "";
try
{
str = service.say("Libin");
}
catch (Exception we)
{
str = we.Message;
}
//MessageBox.Show(str);
textBox1.Text = str ;
}
private void Form1_Load(object sender, EventArgs e)
{
service = new WebServiceSampleImplService();
label1.Text = "WS.url:" + service.Url + "\r\n";
}
private void button2_Click(object sender, EventArgs e)
{
String str = "";
try
{
User user = new User();
user.id = 1;
user.name = "Libin";
str = "userName:" + service.sayUserName(user);
}
catch (Exception we)
{
str = we.Message;
}
//MessageBox.Show(str);
textBox1.Text = str;
}
private void button3_Click(object sender, EventArgs e)
{
String str = "";
try
{
Object[] users = service.findUsers();
for (int i = 0; i < users.Length; i++)
{
User user = (User)users[i];
str += "id:"+user.id + " name:" + user.name + "\r\n";
}
}
catch (Exception we)
{
str = we.Message;
}
//MessageBox.Show(str);
textBox1.Text = str;
}
}
}
6.最后点击调试运行,或按F5运行,通过点击按钮就可以看到调用WebService的运行结果了