第二篇调用webservice,web服务是http://webservice.webxml.com.cn/webservices/DomesticAirline.asmx,航班查询服务
添加web服务的方法是:在visual studio中的“解决方案管理器”的项目中有个“引用”,右击“添加服务引用”,在对话框中输入webservice的URL,点击“前往”,再“确定”就好了。
此服务有两个方法: getDomesticAirlinesTime(string,string,string,string) //四个参数,返回DataSet
getDomesticCity() //无参数,返回DataSet
1.用了两个listview来显示结果
listview的使用:添加列标题
1 listView1.View = View.Details; 2 listView1.Columns.Add("航空公司", -2, HorizontalAlignment.Left); 3 listView1.Columns.Add("航班号", -2, HorizontalAlignment.Left);
2.要用的命名空间:
1 using System.Net; 2 using System.Data;
3.添加控件:
1 private void button3_Click(object sender, EventArgs e) 2 { 3 ServiceReference2.DomesticAirlineSoapClient ww = new ServiceReference2.DomesticAirlineSoapClient("DomesticAirlineSoap"); 4 DataSet s = new DataSet(); 5 s = ww.getDomesticAirlinesTime(textBox2.Text,textBox3.Text,textBox4.Text,string.Empty); 6 int RowCount = s.Tables[0].Rows.Count; 7 int ColumnCount = s.Tables[0].Columns.Count; 8 for (int i = 0; i < RowCount; i++) 9 { 10 string itemName = s.Tables[0].Rows[i][0].ToString(); 11 ListViewItem item = new ListViewItem(itemName, i); 12 //循环每一列 13 for (int j = 1; j < ColumnCount; j++) 14 { 15 item.SubItems.Add(s.Tables[0].Rows[i][j].ToString()); 16 } 17 listView1.Items.Add(item); 18 } 19 } 20 private void button4_Click(object sender, EventArgs e) 21 { 22 ServiceReference2.DomesticAirlineSoapClient ww = new ServiceReference2.DomesticAirlineSoapClient("DomesticAirlineSoap"); 23 DataSet s = new DataSet(); 24 s = ww.getDomesticCity(); 25 int RowCount = s.Tables[0].Rows.Count; 26 int ColumnCount = s.Tables[0].Columns.Count; 27 for (int i = 0; i < RowCount; i++) 28 { 29 string itemName = s.Tables[0].Rows[i][0].ToString(); 30 ListViewItem item = new ListViewItem(itemName, i); 31 //循环每一列 32 for (int j = 1; j < ColumnCount; j++) 33 { 34 item.SubItems.Add(s.Tables[0].Rows[i][j].ToString()); 35 } 36 listView2.Items.Add(item); 37 } 38 }
4.最后结果:(和上一次的弄一起了)