轉載地址:
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);