下面我们生成一个web service,起名叫FileServer,在FileServer.asmx中有如下代码:
<%@ WebService Language="c#" Codebehind="FileServer.asmx.cs" Class="useResData.FileServer" %>
大家可以看到Codebehind技术是如何被使用的。在Visual Studio.Net中,自动生成的代码大量使用这样的语句。它使得设计页面和编写代码被划分开了。
在FileServer.asmx.cs中,代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.IO;
namespace useResData
{
///
/// Summary description for FileServer.
///
public class FileServer : System.Web.Services.WebService
{private string rootdir;
public FileServer()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
rootdir=Server.MapPath("/caomo/提供传输的文件");
}
#region Component Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
}
#endregion
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
}
public Authentication header; //定义用户身份验证类变量header。
[WebMethod(Description="need authentication!")]
[System.Web.Services.Protocols.SoapHeader("header")]
//用户身份验证的soap头
public string GetFile(string filePath)
{
if (header.ValidUser(header.Username,header.Password)) //用户身份验证
{
FileStream myfile=File.OpenRead(rootdir+filePath);
BinaryReader br=new BinaryReader(myfile);
byte[] btBuf=new byte[myfile.Length];
long i=0;
while (br.PeekChar()>-1)
{
btBuf[i]=br.ReadByte();
i++;
}
myfile.Close();
return System.Convert.ToBase64String(btBuf);
}
else return null;//用户身份验证failed
}
运行它。将会得到如图1所示页面:
图 1
大家应该注意到名为GetFile的服务是我给的代码中的Web Method,下面的“need authentication!”是由WebMethod定义中的Description="need authentication!"给出的。文字:http://www.qqread.com/csharp/u303914.html进入讨论组讨论。
<%@ WebService Language="c#" Codebehind="FileServer.asmx.cs" Class="useResData.FileServer" %>
大家可以看到Codebehind技术是如何被使用的。在Visual Studio.Net中,自动生成的代码大量使用这样的语句。它使得设计页面和编写代码被划分开了。
在FileServer.asmx.cs中,代码如下:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.IO;
namespace useResData
{
///
/// Summary description for FileServer.
///
public class FileServer : System.Web.Services.WebService
{private string rootdir;
public FileServer()
{
//CODEGEN: This call is required by the ASP.NET Web Services Designer
InitializeComponent();
rootdir=Server.MapPath("/caomo/提供传输的文件");
}
#region Component Designer generated code
///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
///
private void InitializeComponent()
{
}
#endregion
///
/// Clean up any resources being used.
///
protected override void Dispose( bool disposing )
{
}
public Authentication header; //定义用户身份验证类变量header。
[WebMethod(Description="need authentication!")]
[System.Web.Services.Protocols.SoapHeader("header")]
//用户身份验证的soap头
public string GetFile(string filePath)
{
if (header.ValidUser(header.Username,header.Password)) //用户身份验证
{
FileStream myfile=File.OpenRead(rootdir+filePath);
BinaryReader br=new BinaryReader(myfile);
byte[] btBuf=new byte[myfile.Length];
long i=0;
while (br.PeekChar()>-1)
{
btBuf[i]=br.ReadByte();
i++;
}
myfile.Close();
return System.Convert.ToBase64String(btBuf);
}
else return null;//用户身份验证failed
}
运行它。将会得到如图1所示页面:
图 1
大家应该注意到名为GetFile的服务是我给的代码中的Web Method,下面的“need authentication!”是由WebMethod定义中的Description="need authentication!"给出的。文字:http://www.qqread.com/csharp/u303914.html进入讨论组讨论。
相关图文阅读
频道图文推荐
健 康 咨 询
时 尚 咨 询
相关专题
- C# 3.0新特性之扩展方法 (1次浏览)
- Windows Shell 开发:从“桌面”开始展开 (0次浏览)
- 实验分析C#中三种计时器使用异同点 (0次浏览)
- C#调用QTP自动化对象模型的实例 (0次浏览)
- 利用 C# 实现任务栏通知窗口 (0次浏览)
- 深入C#学习系列之不可小瞧的using关键字 (0次浏览)
- 如何用.NET技术在线生成网站LOGO (0次浏览)
- C#实现遗传算法 模拟花朵的进化 (0次浏览)
- c#里的运算符重载 (0次浏览)
- QTP调用自己开发的.net类库 (0次浏览)



