关于WebService的声明部分
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.IO;
namespace WebServiceSkydemo
{
/// <summary>
/// Service1 的摘要说明
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[ToolboxItem(false)]
// 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务,请取消对下行的注释。
// [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
private string rootDir;
public Authentication header;
public Service1()
{
rootDir = Server.MapPath("/test");//服务器端路径映射 ,在IIS中定义一个虚拟路径/test,指向硬盘上的特定目录
}
[WebMethod(Description = "Need Authentication!")]
[System.Web.Services.Protocols.SoapHeader("header")]
public string GetFile(string filePath)
{
if (header.ValidateUser(header.UserName, header.PassWord) ||
header.ValidateAdmin(header.UserName, header.PassWord))
{
FileStream myFile = File.OpenRead(rootDir + '\\' + filePath);
BinaryReader br = new BinaryReader(myFile);
byte[] btbuf = new byte[myFile.Length];
for (int i = 0; i < myFile.Length; i++)
{
btbuf[i] = br.ReadByte();
}
return Convert.ToBase64String(btbuf);
}
else
{
return "";
}
}
[WebMethod(Description = "Need Authentication!")]
[System.Web.Services.Protocols.SoapHeader("header")]
public string ChangePassword(string newPass)//修改密码
{
string user="";
if (header.ValidateUser(header.UserName, header.PassWord) ||
header.ValidateAdmin(header.UserName, header.PassWord))
{
user = header.UserName;
}
else
{
return "没有找到该用户或密码错误";
}
if (header.ChangePassWord(user, newPass))
{
return "修改成功";
}
else
{
return "修改失败";
}
}
[WebMethod(Description="Need Authentication!")]
[System.Web.Services.Protocols.SoapHeader("header")]
public string AddUser(string userName, string passWord, string auth)
{
if (header.ValidateAdmin(header.UserName, header.PassWord))
{
header.AddUser(userName, passWord, auth);
return "添加成功!";
}
else
{
return "添加失败";
}
}
[WebMethod]
public string[] GetList()//获取目录列表
{
DirectoryInfo info = new DirectoryInfo(rootDir);
string[] oc = new string[info.GetFiles().Length];
int i = 0;
foreach (FileInfo fileInfo in info.GetFiles())
{
oc[i] = fileInfo.Name;
i++;
}
return oc;
}
}
}