WP7中网络编程之调用web service

1 篇文章 0 订阅
1 篇文章 0 订阅

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

具体步骤为:

一.选中项目单击添加服务引用,如下图所示:

                                                                      

二.在对话框中做如下操作,如图所示:

                                                   

第二步:引用添加完毕会在项目中增加如下信息:

                                                                                     

调用WebServer服务的代码,如下所示:

Xaml中的代码信息为:

            <TextBlock Height="30" HorizontalAlignment="Left" Margin="66,65,0,0" Name="textBlock1" Text="请输入手机号码" VerticalAlignment="Top" Grid.ColumnSpan="3" />

            <TextBox Height="72" HorizontalAlignment="Left" Margin="40,101,0,0" Name="txtPhoneNum" Text="" VerticalAlignment="Top" Width="392" Grid.ColumnSpan="3" />

            <Button Content="查  询"  Height="72" HorizontalAlignment="Left" Margin="9,213,0,0" Name="btnCheck" VerticalAlignment="Top" Width="160" Click="btnCheck_Click" Grid.Column="2" />

            <TextBlock Height="40" HorizontalAlignment="Left" Margin="40,299,0,0" Name="lbDisplayNum" VerticalAlignment="Top" Width="392" Text="" Grid.ColumnSpan="3" />

        </Grid>

        /// <summary>

        /// 信息查询按键

        /// </summary>

        /// <param name="sender"></param>

        /// <param name="e"></param>

        private void btnCheck_Click(object sender, RoutedEventArgs e)

        {

            this.JudgeInfo();                              // 判断输入信息的合法性

        }

        /// <summary>

        /// 判断输入信息的合法性

        /// </summary>

        /// 判断输入信息位数合法

        /// 判断输入信息内容合法

        private void JudgeInfo()

        {

            string phoneNum=txtPhoneNum.Text;

            if (string.IsNullOrEmpty(phoneNum))

            {

                MessageBox.Show("信息不能为空!");

                return;

            }

            else if (phoneNum.Length<11)

            {

                MessageBox.Show("号码输入位数不正确,请重新输入!");

                txtPhoneNum.Focus();

                txtPhoneNum.Text =null;

                return;

            }

            //信息合法则直接进行数据显示

            this.GetInfoAboutPhoneNum();

        }

        /// <summary>

        /// 获取网络信息

        /// </summary>

        /// 使用代理服务的事件和方法

        private void GetInfoAboutPhoneNum()

        {

            //实例化web service代理的对象

            MobileReference.MobileCodeWSSoapClient webpro = new MobileReference.MobileCodeWSSoapClient();

            

            //重新实现getMobileCodeInfo方法调用结束之后 触发的事件

            webpro.getMobileCodeInfoCompleted += new EventHandler<MobileReference.getMobileCodeInfoCompletedEventArgs>(webpro_getMobileCodeInfoCompleted);

           

            //将调用信息包括方法名和参数加入到soap消息中通过http传送给web service服务端  

            //这里对应的是调用了web service的getMobileCodeInfo方法

            //需要用户号码号码信息,用户id参数可以不输入,若输入错误将进行错误提示,

            //webpro_getMobileCodeInfoCompleted中课捕获错误信息

            webpro.getMobileCodeInfoAsync(txtPhoneNum.Text, "");

        }

        void webpro_getMobileCodeInfoCompleted(object sender, MobileReference.getMobileCodeInfoCompletedEventArgs e)

        {

            if (e.Error == null)

            {

                //显示返回结果信息

                lbDisplayNum.Text = e.Result;

            }

            else

            {

                lbDisplayNum.Text = "错误提示:\n" + e.Result;

            }

        }

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值