PDA(Windows Mobile)調用遠程WebService


之前用模擬器測試過調用遠程的WebService,發現總是提示“無法連接到遠程服務器”的錯誤,不管是Windows Mobile6.0 還是6.5都是一樣,按照網上的辦法,改注冊表,修改PDA的配置,安裝虛擬網卡,我一一試了一遍,
都沒有解決,可能是模擬器需要什么特殊的配置吧,晚一點繼續摸索一下,現在先使用真機來測試一下。

1.首先新建測試的WebService服務,並將其發布在IIS或者服務器上面,我這里做了兩個測試,一個是發布到本地IIS里面,一個是發布到服務器上面。

以下是我建立的兩個測試Web服務。

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Services;
/// <summary>
///WebService 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class WebService : System.Web.Services.WebService
{
    public WebService()
    {

        //如果使用設計的組件,請取消注釋以下行 
        //InitializeComponent(); 
    }

    [WebMethod]
    public string GetString(string Num1,string Num2)
    {
        return (Convert.ToInt32(Num1) + Convert.ToInt32(Num2)).ToString();
    }    

}



/// <summary>
///WebServices 的摘要說明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
//若要允許使用 ASP.NET AJAX 從腳本中調用此 Web 服務,請取消對下行的注釋。 
// [System.Web.Script.Services.ScriptService]
//public class WebServices : System.Web.Services.WebService 
public class WebServices : DBHelper
{

    public WebServices()
    {

        //如果使用設計的組件,請取消注釋以下行 
        //InitializeComponent(); 
    }

  

    [WebMethod(Description = "檢查用戶是否存在")]
    public bool CheckUserExists(string UserName)
    {
        bool result = false;
        string strSql = "SELECT COUNT(*) FROM SYS_UserInfo WHERE UserName='" + UserName + "'";

        int i = Convert.ToInt32(base.GetScalar(strSql).ToString());
        if (i > 0)
        {
            result = true;
        }
        return result;
    }
}

這里就省略掉發布的過程了,先說一下新建智能設備程序的步驟:
1.新建項目--智能設備--智能設備項目--設置項目名稱以及解決方案名稱--確定

2.選擇目標平台,這里有 Pokcet PC,Windows CE,Windows Mobile 5.0 SDK,Windows Mobile 6.0 SDK。具體的還要看

PC上面安裝了哪些SDK。

3.選擇.Net Compact FrameWork 版本,2.0/3.0等等。

4.選擇項目模板,表示新建的項目是控制台程序還是引用程序或者是類庫等等。


現在要做的就是在程序里面調用了,先將我們的WebService引用到項目中來,
項目--右鍵--添加Web引用--輸入服務器IP,如果是本地的話直接點擊“本地計算機上面的Web服務”--自己給它命名

並點擊“添加引用”按鈕,


操作成功之后可以在Web References里面看到我們的WebService了,

 

private void button1_Click(object sender, EventArgs e)
        {
            WebReference.WebServices wbs = new PPCWebS.WebReference.WebServices();//服務器上面的WebService
            bool result = wbs.CheckUserExists(this.txtServerValue.Text.Trim());
            if (result)
            {
                MessageBox.Show("用戶:" + this.txtServerValue.Text.Trim() + "存在數據庫中");
            }
            else
            {
                MessageBox.Show("用戶:" + this.txtServerValue.Text.Trim() + "不存在數據庫中");
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            WebReference2.WebService w = new PPCWebS.WebReference2.WebService();//本地IIS里面的WebService
            string Result = w.GetString(this.txtLocalhost1.Text.Trim(), this.txtLocalhost2.Text.Trim());
            MessageBox.Show(this.txtLocalhost1.Text.Trim() + "+" + this.txtLocalhost2.Text.Trim() + "=" + Result);
        }

 


小技巧:通過 Windows CE Remote Zoom-in(遠程放大)可以捕獲到智能設備的屏幕


開發的環境:IIS V5.1
             Visual Studio 2008專業版
                Windows Mobile 6.1 智能設備
                 Windows Mobile 6.0 SDK


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM