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界面代码

<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<TextBlock Height="49" HorizontalAlignment="Left" Margin="12,66,0,0" Name="des" Text="请输入你需要查询的手机号码" VerticalAlignment="Top" Width="284" />
<TextBox Height="72" HorizontalAlignment="Left" Margin="6,106,0,0" Name="No" Text="" VerticalAlignment="Top" Width="415" />
<Button Content="查询" Height="72" HorizontalAlignment="Left" Margin="12,184,0,0" Name="search" VerticalAlignment="Top" Width="160" Click="search_Click" />
<TextBlock Height="211" HorizontalAlignment="Left" Margin="6,277,0,0" Name="information" Text="" VerticalAlignment="Top" Width="444" />
</Grid>

调用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<MobileReference.getMobileCodeInfoCompletedEventArgs>(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。。。。

posted on 2011-04-21 19:37  linzheng  阅读(3219)  评论(1编辑  收藏  举报

导航