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