Windows Phone 7 网络编程之调用web service
下面通过一个手机号码归属地查询例子来演示Windows Phone 7的应用程序如何调用web service 接口。先看一下运行的效果:
应用调用的手机号码归属地查询的web service接口为:
http://webservice.webxml.com.cn/WebServices/MobileCodeWS.asmx
第一步添加webservice的引用,将web service服务加入,这时生成了上述web服务在本地的一个代理。
由于.net平台内建了对Web Service的支持,包括Web Service的构建和使用,所以在Windows Phone 7项目中你不需要其他的工具或者SDK就可以完成Web Service的开发了。
添加web service引用后,项目的文件目录如下:
多了MobileReference服务和ServiceReferences.ClientConfig文件
第二步调用web service
先看一下XAML界面代码
调用web service服务,代码很简洁。。。
public partial class MainPage : PhoneApplicationPage
{
public MainPage()
{
InitializeComponent();
}
private void search_Click(object sender, RoutedEventArgs e)
{
//实例化一个web service代理的对象
MobileReference.MobileCodeWSSoapClient proxy = new MobileReference.MobileCodeWSSoapClient();
//getMobileCodeInfo方法调用结束之后 触发的事件
proxy.getMobileCodeInfoCompleted+=new EventHandler(proxy_getMobileCodeInfoCompleted);
//将调用信息包括方法名和参数加入到soap消息中通过http传送给web service服务端
//这里对应的是调用了web service的getMobileCodeInfo方法
proxy.getMobileCodeInfoAsync(No.Text, "");
}
void proxy_getMobileCodeInfoCompleted(object sender, MobileReference.getMobileCodeInfoCompletedEventArgs e)
{
if (e.Error == null)
{
//显示返回的结果
information.Text = e.Result;
}
}
}
ok。。。。
页:
[1]