HttpRequest.Filter 属性
2011-04-25 15:13
435 查看
获取或设置在读取当前输入流时要使用的筛选器。
下面的代码示例创建两个新类,即,筛选 InputStream 的 QQQ1 和 QQQ2。将这些类放入 ASP.NET 应用程序目录中的 Global.asax 文件中,以便筛选应用程序中所有 ASP.NET 网页中的全部输入。
<%@ Page language="c#" %>
<%@ Import namespace="System.Text" %>
<%@ Import namespace="System.IO" %> <script runat="server">
// This code is to be added to a Global.asax file.
public void Application_BeginRequest() { Request.Filter = new QQQ1(Request.Filter);
Request.Filter = new QQQ2(Request.Filter);
}
class QQQ1 : Stream
{
private Stream _sink;
public QQQ1(Stream sink)
{
_sink = sink;
}
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return false; }
}
public override long Length
{
get { return _sink.Length; }
}
public override long Position
{
get { return _sink.Position; }
set { throw new NotSupportedException(); }
}
public override int Read(byte[] buffer, int offset, int count)
{
int c = _sink.Read(buffer, offset, count);
for (int i = 0; i < count; i++)
{
if (buffer[offset+i] >= 'a' && buffer[offset+i] <= 'z')
buffer[offset+i] -= ('a'-'A');
}
return c;
}
public override long Seek(long offset, System.IO.SeekOrigin direction)
{
throw new NotSupportedException();
}
public override void SetLength(long length)
{
throw new NotSupportedException();
}
public override void Close()
{
_sink.Close();
}
public override void Flush()
{
_sink.Flush();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
}
class QQQ2 : Stream
{
private Stream _sink;
public QQQ2(Stream sink)
{
_sink = sink;
}
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return false; }
}
public override long Length
{
get { return _sink.Length; }
}
public override long Position
{
get { return _sink.Position; }
set { throw new NotSupportedException(); }
}
public override int Read(byte[] buffer, int offset, int count)
{
int c = _sink.Read(buffer, offset, count);
for (int i = 0; i < count; i++)
{
if (buffer[i] == 'E')
buffer[i] = (byte)'*';
else if (buffer[i] == 'e')
buffer[i] = (byte)'#';
}
return c;
}
public override long Seek(long offset, System.IO.SeekOrigin direction)
{
throw new NotSupportedException();
}
public override void SetLength(long length)
{
throw new NotSupportedException();
}
public override void Close()
{
_sink.Close();
}
public override void Flush()
{
_sink.Flush();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
}
/*____________________________________________________________________
This ASP.NET page uses the request filter to modify all text sent by the
browser in Request.InputStream. To test the filter, use this page to take
the POSTed output from a data entry page using a tag such as:
<form method="POST" action="ThisTestPage.aspx">
<%@ PAGE LANGUAGE = C# %>
<%@ IMPORT namespace="System.IO" %>
<html>
<Script runat=server>
void Page_Load()
{
// Create a Stream object to capture entire InputStream from browser.
Stream str = Request.InputStream;
// Find number of bytes in stream.
int strLen = (int)str.Length;
// Create a byte array to hold stream.
byte[] bArr = new byte[strLen];
// Read stream into byte array.
str.Read(bArr,0,strLen);
// Convert byte array to a text string.
String strmContents="";
for(int i = 0; i < strLen; i++)
strmContents = strmContents + (Char)bArr[i];
// Display filtered stream in browser.
Response.Write("Contents of Filtered InputStream: <br>" + strmContents);
}
______________________________________________________________________*/
</script>
下面的代码示例创建两个新类,即,筛选 InputStream 的 QQQ1 和 QQQ2。将这些类放入 ASP.NET 应用程序目录中的 Global.asax 文件中,以便筛选应用程序中所有 ASP.NET 网页中的全部输入。
<%@ Page language="c#" %>
<%@ Import namespace="System.Text" %>
<%@ Import namespace="System.IO" %> <script runat="server">
// This code is to be added to a Global.asax file.
public void Application_BeginRequest() { Request.Filter = new QQQ1(Request.Filter);
Request.Filter = new QQQ2(Request.Filter);
}
class QQQ1 : Stream
{
private Stream _sink;
public QQQ1(Stream sink)
{
_sink = sink;
}
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return false; }
}
public override long Length
{
get { return _sink.Length; }
}
public override long Position
{
get { return _sink.Position; }
set { throw new NotSupportedException(); }
}
public override int Read(byte[] buffer, int offset, int count)
{
int c = _sink.Read(buffer, offset, count);
for (int i = 0; i < count; i++)
{
if (buffer[offset+i] >= 'a' && buffer[offset+i] <= 'z')
buffer[offset+i] -= ('a'-'A');
}
return c;
}
public override long Seek(long offset, System.IO.SeekOrigin direction)
{
throw new NotSupportedException();
}
public override void SetLength(long length)
{
throw new NotSupportedException();
}
public override void Close()
{
_sink.Close();
}
public override void Flush()
{
_sink.Flush();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
}
class QQQ2 : Stream
{
private Stream _sink;
public QQQ2(Stream sink)
{
_sink = sink;
}
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return false; }
}
public override long Length
{
get { return _sink.Length; }
}
public override long Position
{
get { return _sink.Position; }
set { throw new NotSupportedException(); }
}
public override int Read(byte[] buffer, int offset, int count)
{
int c = _sink.Read(buffer, offset, count);
for (int i = 0; i < count; i++)
{
if (buffer[i] == 'E')
buffer[i] = (byte)'*';
else if (buffer[i] == 'e')
buffer[i] = (byte)'#';
}
return c;
}
public override long Seek(long offset, System.IO.SeekOrigin direction)
{
throw new NotSupportedException();
}
public override void SetLength(long length)
{
throw new NotSupportedException();
}
public override void Close()
{
_sink.Close();
}
public override void Flush()
{
_sink.Flush();
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new NotSupportedException();
}
}
/*____________________________________________________________________
This ASP.NET page uses the request filter to modify all text sent by the
browser in Request.InputStream. To test the filter, use this page to take
the POSTed output from a data entry page using a tag such as:
<form method="POST" action="ThisTestPage.aspx">
<%@ PAGE LANGUAGE = C# %>
<%@ IMPORT namespace="System.IO" %>
<html>
<Script runat=server>
void Page_Load()
{
// Create a Stream object to capture entire InputStream from browser.
Stream str = Request.InputStream;
// Find number of bytes in stream.
int strLen = (int)str.Length;
// Create a byte array to hold stream.
byte[] bArr = new byte[strLen];
// Read stream into byte array.
str.Read(bArr,0,strLen);
// Convert byte array to a text string.
String strmContents="";
for(int i = 0; i < strLen; i++)
strmContents = strmContents + (Char)bArr[i];
// Display filtered stream in browser.
Response.Write("Contents of Filtered InputStream: <br>" + strmContents);
}
______________________________________________________________________*/
</script>
相关文章推荐
- Struts2 Filter 和 HttpServletRequestWrapper出现的问题
- XMLHttpRequest对象的属性和方法
- XMLHttpRequest对象方法和属性及实现AJAX标准示例
- ajax-XMLHttpRequest()对象,属性及方法
- 关于xmlhttprequest的readystate属性的五个状态(转载)
- js中XMLHttpRequest属性和方法
- XMLHttpRequest对象responseText属性的兼容性问题
- JMeter学习(二十五)HTTP属性管理器HTTP Cookie Manager、HTTP Request Defaults
- XMLHttpRequest 对象的方法与属性
- XMLHTTPRequest的属性和方法简介
- XMLHttpRequest对象的属性
- Ajax XMLHttpRequest对象的三个属性以及open和send方法
- XMLHttpRequest对象的方法和属性
- asp.net 中 HttpRequest 中跟URL path 有关的方法和属性
- Ajax XMLHttpRequest对象的三个属性以及open和send方法
- XMLHttpRequest对象的常用属性与方法
- ajax学习笔记---XMLHttpRequest的属性和方法
- Ajax - XMLHttpRequest对象的属性responseText
- XMLHttpRequest upload属性
- HttpRequest.QueryString 属性