성태의 닷넷 이야기
홈 주인
모아 놓은 자료
프로그래밍
질문/답변
사용자 관리
사용자
메뉴
아티클
외부 아티클
유용한 코드
온라인 기능
MathJax 입력기
최근 덧글
[정성태] Java - How to use the Foreign Funct...
[정성태] 제가 큰 실수를 했군요. ^^; Delegate를 통한 Bein...
[정성태] Working with Rust Libraries from C#...
[정성태] Detecting blocking calls using asyn...
[정성태] 아쉽게도, 커뮤니티는 아니고 개인 블로그입니다. ^^
[정성태] 질문이 잘 이해가 안 됩니다. 우선, 해당 소스코드에서 ILis...
[양승조
] var대신 dinamic으로 선언해서 해결은 했습니다. 맞는 해...
[양승조
] 또 막혔습니다. ㅠㅠ var list = props[i].Ge...
[양승조
] 아. 감사합니다. 어제는 안됐던것 같은데....정신을 차려야겠네...
[정성태] "props[i].GetValue(props[i])" 코드에서 ...
글쓰기
제목
이름
암호
전자우편
HTML
홈페이지
유형
제니퍼 .NET
닷넷
COM 개체 관련
스크립트
VC++
VS.NET IDE
Windows
Team Foundation Server
디버깅 기술
오류 유형
개발 환경 구성
웹
기타
Linux
Java
DDK
Math
Phone
Graphics
사물인터넷
부모글 보이기/감추기
내용
<div style='display: inline'> <h1 style='font-family: Malgun Gothic, Consolas; font-size: 20pt; color: #006699; text-align: center; font-weight: bold'>C# - ASP.NET 웹 응용 프로그램의 출력 가로채기</h1> <p> ASP.NET의 출력을 가로채는 것은 HttpResponse.Filter 속성을 이용해 쉽게 구현할 수 있습니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > HttpResponse.Filter Property ; <a target='tab' href='https://learn.microsoft.com/en-us/dotnet/api/system.web.httpresponse.filter?view=netframework-4.8'>https://learn.microsoft.com/en-us/dotnet/api/system.web.httpresponse.filter?view=netframework-4.8</a> </pre> <br /> 따라서 BeginRequest (또는 Page_Load 등의 원하는) 시점에 다음과 같이 Filter 속성을 대체해 주고,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Security; using System.Web.SessionState; namespace WebApplication2 { public class Global : System.Web.HttpApplication { protected void Application_Start(object sender, EventArgs e) { } protected void Session_Start(object sender, EventArgs e) { } protected void Application_BeginRequest(object sender, EventArgs e) { HttpContext context = HttpContext.Current; <span style='color: blue; font-weight: bold'>context.Response.Filter = new FilterStream(context.Response.Filter, context.Response.ContentEncoding);</span> } protected void Application_AuthenticateRequest(object sender, EventArgs e) { } protected void Application_Error(object sender, EventArgs e) { } protected void Session_End(object sender, EventArgs e) { } protected void Application_End(object sender, EventArgs e) { } } } </pre> <br /> 출력을 가로챈 클래스의 요건은 Stream 클래스만 상속받으면 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Web; namespace WebApplication2 { public class <span style='color: blue; font-weight: bold'>FilterStream : Stream</span> { private readonly Stream _orginalStream; private readonly Encoding _encoding; public FilterStream(Stream originalStream, Encoding encoding) { <span style='color: blue; font-weight: bold'>_orginalStream = originalStream</span>; _encoding = encoding; } public override bool CanRead => _orginalStream.CanRead; public override bool CanSeek => _orginalStream.CanSeek; public override bool CanWrite => _orginalStream.CanWrite; public override long Length => _orginalStream.Length; public override long Position { get => _orginalStream.Position; set => _orginalStream.Position = value; } public override void Flush() { _orginalStream.Flush(); } public override int Read(byte[] buffer, int offset, int count) { return _orginalStream.Read(buffer, offset, count); } public override long Seek(long offset, SeekOrigin origin) { return _orginalStream.Seek(offset, origin); } public override void SetLength(long value) { _orginalStream.SetLength(value); } <span style='color: blue; font-weight: bold'>public override void Write(byte[] buffer, int offset, int count) { _orginalStream.Write(buffer, offset, count); }</span> } } </pre> <br /> 중요한 것은 Write 메서드인데, 입력으로 받는 buffer를 _orginalStream.Write 메서드에 어떻게 전달하느냐에 따라 추가/삭제/변경 등을 할 수 있습니다. 예를 들어 다음과 같이 구현하면,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > public override void Write(byte[] buffer, int offset, int count) { _orginalStream.Write(buffer, offset, count); <span style='color: blue; font-weight: bold'>byte[] buf = _encoding.GetBytes("TEST"); _orginalStream.Write(buf, 0, buf.Length);</span> } </pre> <br /> Write 메서드가 발생할 때마다, 즉 ASP.NET의 내부 동작에 의해 출력되는 HTTP chunk마다 마지막에 "TEST"라는 문자열이 추가됩니다. 따라서, 다음과 같이 (대개의 경우) 한 번의 chunk로 출력되는 작은 웹 페이지는,<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication2.Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> test is good </div> </form> </body> </html> </pre> <br /> 출력 시 다음과 같이 "TEST" 문자열이 마지막에 붙는 식으로 동작하게 됩니다.<br /> <br /> <pre style='margin: 10px 0px 10px 10px; padding: 10px 0px 10px 10px; background-color: #fbedbb; overflow: auto; font-family: Consolas, Verdana;' > <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head><title> </title></head> <body> <form method="post" action="./" id="form1"> <div class="aspNetHidden"> <input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwULLTE2MTY2ODcyMjlkZLcQMOQaGGJkAZFI3/Vw49ifxpRjSfnundrgHmYnfO7H" /> </div> <div class="aspNetHidden"> <input type="hidden" name="__VIEWSTATEGENERATOR" id="__VIEWSTATEGENERATOR" value="CA0B0334" /> </div> <div> test is good </div> </form> </body> </html> <span style='color: blue; font-weight: bold'>TEST</span> </pre> <br /> 여기서, chunk 단위라는 점을 주의해야 하는데, 이 때문에 일부 내용을 변경해야 하는 식이라면 chunk에 걸쳐서 출력이 되는 것도 감안하거나 아예 로직의 단순함을 위해 그런 부분은 포기하는 선택을 해야 합니다. 게다가, 이미 인코딩이 끝난 byte [] 형식이기 때문에 변경해야 하는 문자열이 ascii가 아니라면 인코딩 방식에 따라 꽤나 복잡한 작업이 될 수 있습니다.<br /> <br /> (<a target='tab' href='https://www.sysnet.pe.kr/bbs/DownloadAttachment.aspx?fid=1549&boardid=331301885'>첨부 파일은 이 글의 예제 코드를 포함</a>합니다.)<br /> </p><br /> <br /><hr /><span style='color: Maroon'>[이 글에 대해서 여러분들과 의견을 공유하고 싶습니다. 틀리거나 미흡한 부분 또는 의문 사항이 있으시면 언제든 댓글 남겨주십시오.]</span> </div>
첨부파일
스팸 방지용 인증 번호
4709
(왼쪽의 숫자를 입력해야 합니다.)