WebService開發(fā)筆記 2 -- VS 2005 訪問WebServcie更簡(jiǎn)單
在上一回中我們創(chuàng)建了一個(gè)WebService服務(wù)(WebService開發(fā)筆記 1 -- 利用cxf開發(fā)WebService竟然如此簡(jiǎn)單 ),下面就來作一個(gè)跨平臺(tái)訪問WebServcie服務(wù)的例子.... 下面將在vs2005中通過c#.net訪問我們創(chuàng)建好的WebService服務(wù),,C#.net第一次用,,TNN的沒想到這么簡(jiǎn)單,MS就是MS,不服不行,。 1.首先在vs中新建項(xiàng)目 2.添加web引用.... 3.輸入WebService地址:http://88.148.29.54:8080/aio/services/WebServiceSample?wsdl最后點(diǎn)擊“添加引用” 4.創(chuàng)建一個(gè)簡(jiǎn)單的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.最后點(diǎn)擊調(diào)試運(yùn)行,,或按F5運(yùn)行,,通過點(diǎn)擊按鈕就可以看到調(diào)用WebService的運(yùn)行結(jié)果了 |
|