转载地址:
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);