|
最近做服务器,需要获取天气信息,想到了Web Service,以前还没有用过,这次用了之后感觉很不错,就是相当于远程的函数调用,记录如下。
1. 添加winform工程MyTest01,在右边“解决方案资源管理器”的工程文件上点击右键,选择“添加Web引用”.
2. 在弹出的“添加web引用”对话框中,URL地址里面输入提供web Service的服务器地址,http://www.webxml.com.cn/WebServices/WeatherWebService.asmx,点击“前往”按钮,系统提示“找到1个服务:WeatherWebService”,将我们使用的Web引用名修改为weather,这样子不用系统默认那么长的一大串名字。添加该引用。
3. 系统会自动打开“对象浏览器”,选择MyTest01.Weather,看看这个WebService都有哪些方法吧。
4.Form1窗口界面上,添加如下控件:
5.在Form1.vb里面添加代码,实现天气预报的功能。
Imports MyTest01.Weather
Public Class Form1
Dim MyWeather As Weather.WeatherWebService = New Weather.WeatherWebService()
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim tmpStr(0 To 23) As String
tmpStr = MyWeather.getWeatherbyCityName(Cmb_City.Text.Substring(0, Cmb_City.Text.IndexOf(" ")))
If (tmpStr(8) = "") Then
MessageBox.Show("暂时不支持您查询的城市")
Else
Label1.Text = "天气概况:" & tmpStr(1) & " " & tmpStr(6)
TextBox1.Text = tmpStr(10)
TextBox2.Text = tmpStr(11)
TextBox3.Text = tmpStr(13) & "," & tmpStr(12) & "," & tmpStr(14) & vbCrLf & tmpStr(18) & "," & tmpStr(17) & "," & tmpStr(19)
TextBox4.Text = tmpStr(22)
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim ProvinceStr() As String
Dim i As Integer
ProvinceStr = MyWeather.getSupportProvince()
For i = ProvinceStr.GetLowerBound(0) To ProvinceStr.GetUpperBound(0)
Cmb_Province.Items.Add(ProvinceStr(i))
Next
Cmb_Province.Text = ProvinceStr(0)
End Sub
Private Sub Cmb_Province_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cmb_Province.SelectedIndexChanged
Dim CityStr() As String
Dim i As Integer
CityStr = MyWeather.getSupportCity(Cmb_Province.Text)
For i = CityStr.GetLowerBound(0) To CityStr.GetUpperBound(0)
Cmb_City.Items.Add(CityStr(i))
Next
Cmb_City.Text = CityStr(0)
End Sub
End Class
6.运行结果:
好玩的实用的WebService还有很多,不过好多都需要掏钱了。
|
|