且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

Windows Phone 7 网络编程之调用web service

更新时间:2022-10-03 17:38:40

下面通过一个手机号码归属地查询例子来演示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的开发了。

 

Windows Phone 7 网络编程之调用web service 

Just have a little faith.

Windows Phone 7 网络编程之调用web service

下面通过一个手机号码归属地查询例子来演示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的开发了。

Windows Phone 7 网络编程之调用web service

添加web service引用后,项目的文件目录如下:

 

Windows Phone 7 网络编程之调用web service 

多了MobileReference服务和ServiceReferences.ClientConfig文件

第二步 调用web service

先看一下XAML界面代码

 


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

调用web service服务,代码很简洁。。。

 


  1. public partial class MainPage : PhoneApplicationPage  
  2.     {  
  3.         public MainPage()  
  4.         {  
  5.             InitializeComponent();  
  6.         }  
  7.  
  8.         private void search_Click(object sender, RoutedEventArgs e)  
  9.         {  
  10.             //实例化一个web service代理的对象  
  11.             MobileReference.MobileCodeWSSoapClient proxy = new MobileReference.MobileCodeWSSoapClient();  
  12.             //getMobileCodeInfo方法调用结束之后 触发的事件  
  13.             proxy.getMobileCodeInfoCompleted+=new EventHandler<MobileReference.getMobileCodeInfoCompletedEventArgs>(proxy_getMobileCodeInfoCompleted);  
  14.             //将调用信息包括方法名和参数加入到soap消息中通过http传送给web service服务端    
  15.             //这里对应的是调用了web service的getMobileCodeInfo方法  
  16.             proxy.getMobileCodeInfoAsync(No.Text, "");  
  17.         }  
  18.  
  19.         void proxy_getMobileCodeInfoCompleted(object sender, MobileReference.getMobileCodeInfoCompletedEventArgs e)  
  20.         {  
  21.             if (e.Error == null)  
  22.             {  
  23.                 //显示返回的结果  
  24.                 information.Text = e.Result;  
  25.             }  
  26.         }  
  27.     } 

ok。。。。



本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1078613