public override int Read(byte[] buffer, int offset, int count)
{
int c = _sink.Read(buffer, offset, count);
string aaa = Encoding.UTF8.GetString(buffer, offset, count);
aaa = aaa.Replace("%27", "%27%27");
byte[] buffer1 = new byte[buffer.Length];
buffer1 = Encoding.GetEncoding(System.Web.HttpContext.Current.Response.Charset).GetBytes(aaa);
int d = buffer1.Length - buffer.Length;
buffer = new byte[buffer1.Length];
for (int i = 0; i < buffer1.Length; i++)
{
buffer[i] = buffer1[i];
} return c+d;
}我在使用Request.filter,重载Stream.read方法时出现死循环的状况。非常不解,具体代码如上,请大家帮帮忙!
<%@ 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>