天天看點

Windows Mobile開發之動态調用WebServcie

轉載位址:

http://www.cnblogs.com/lhuser/articles/1508152.html

最近在做一個手機用戶端項目開發.用戶端作業系統是Windows Mobile 6.0 ,伺服器端作業系統是:Windows Server 2003.功能是:擷取手機上錄影機或者照片和視訊通過網絡傳回伺服器端.對于圖檔和視訊傳輸過程,我考慮了兩種方法,一種是在用C/S模式,就是用戶端和伺服器端用Windows Socket傳輸檔案,另一種是在伺服器上建立Web Service來接收用戶端上傳的檔案.因為,伺服器端最終的應用是建立在Web 程式上的,而且我個人對Socket開發不是很熟悉,是以,先擇了用Web Service 的方式.

而應用這種方式要考慮的問題就是程式移植部署時,用戶端程式對Web 服務的動态引用問題.

我的實作方法如下:

用Vs.net添加Web引用後,執行個體化該類,在執行個體化語句中,右擊該類類名,在彈出菜單中選擇"轉到定義",可以看到類似于以下的代碼:

Code
//------------------------------------------------------------------------------
// <auto-generated>
//     此代碼由工具生成。
//     運作庫版本:2.0.50727.42
//
//     對此檔案的更改可能會導緻不正确的行為,并且如果
//     重新生成代碼,這些更改将會丢失。
// </auto-generated>
//------------------------------------------------------------------------------

// 
// 此源代碼是由 Microsoft.CompactFramework.Design.Data 2.0.50727.42 版自動生成。
// 
namespace FileOperation.FileUpload {
    using System.Diagnostics;
    using System.Web.Services;
    using System.ComponentModel;
    using System.Web.Services.Protocols;
    using System;
    using System.Xml.Serialization;
    
    
    /** <remarks/>
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Web.Services.WebServiceBindingAttribute(Name="FileUploadSoap", Namespace="http://tempuri.org/")]
    public partial class FileUpload : System.Web.Services.Protocols.SoapHttpClientProtocol {
        
        /** <remarks/>
        public FileUpload() {
            this.Url = "http://localhost/gpsgoogle/FileUpload.asmx";
        }

        

        /** <remarks/>
        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/HelloWorld", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
        public string HelloWorld() {
            object[] results = this.Invoke("HelloWorld", new object[0]);
            return ((string)(results[0]));
        }
        
        /** <remarks/>
        public System.IAsyncResult BeginHelloWorld(System.AsyncCallback callback, object asyncState) {
            return this.BeginInvoke("HelloWorld", new object[0], callback, asyncState);
        }
        
        /** <remarks/>
        public string EndHelloWorld(System.IAsyncResult asyncResult) {
            object[] results = this.EndInvoke(asyncResult);
            return ((string)(results[0]));
        }
        
        /** <remarks/>
        [System.Web.Services.Protocols.SoapDocumentMethodAttribute("http://tempuri.org/UploadFile", RequestNamespace="http://tempuri.org/", ResponseNamespace="http://tempuri.org/", Use=System.Web.Services.Description.SoapBindingUse.Literal, ParameterStyle=System.Web.Services.Protocols.SoapParameterStyle.Wrapped)]
        public string UploadFile([System.Xml.Serialization.XmlElementAttribute(DataType="base64Binary")] byte[] data, string fileName) {
            object[] results = this.Invoke("UploadFile", new object[] {
                        data,
                        fileName});
            return ((string)(results[0]));
        }
        
        /** <remarks/>
        public System.IAsyncResult BeginUploadFile(byte[] data, string fileName, System.AsyncCallback callback, object asyncState) {
            return this.BeginInvoke("UploadFile", new object[] {
                        data,
                        fileName}, callback, asyncState);
        }
        
        /** <remarks/>
        public string EndUploadFile(System.IAsyncResult asyncResult) {
            object[] results = this.EndInvoke(asyncResult);
            return ((string)(results[0]));
        }
    }
}
           

其中,可以看到該類的構造函數,是一個沒有參數的構造函數,其中函數中用預設值會它的Url字段指派.因些,我們可以給它增加一個帶參數的構造函數,其中參數為Web引用的Url.如下

public FileUpload(string url)
        {
           this.Url = url;
        }
           

這樣做以後,我們可以用如下語句執行個體化該類:

string url = @"http://192.168.0.55/FileUpload.asmx";
            FileUpload.FileUpload fileUpload = new FileOperation.FileUpload.FileUpload(url);