정보 보관 ver1.0

HttpServerUtility.HtmlEncode & HttpServerUtility.HtmlDecode

James Wetzel 2010. 8. 4. 20:07
HttpServerUtility.HtmlEncode(=Server.HtmlEncode)
다음 예제에서는 HTTP로 전송할 문자열을 인코딩합니다.또한 텍스트 "This is a <Test String>."이 들어 있는 TestString이라는 문자열을 인코딩하고, 이를 EncodedString이라는 문자열에 "This is a &lt;Test String&gt;"으로 복사합니다.

String TestString = "This is a <Test String>.";
String EncodedString = Server.HtmlEncode(TestString);
  
HttpServerUtility.HtmlDecode(=Server.HtmlDecode)
URL을 인코딩하면 모든 브라우저에서 URL 문자열의 텍스트를 올바르게 전달할 수 있습니다.일부 브라우저에서는 물음표(?), 앰퍼샌드(&), 슬래시(/), 공백 등의 문자가 잘리거나 손상될 수 있습니다.따라서 이러한 문자는 <a> 태그로 인코딩하거나, 브라우저에서 문자열을 요청 문자열에 삽입하여 다시 보낼 수 있도록 쿼리 문자열로 인코딩해야 합니다.

<%@ PAGE LANGUAGE = "C#" %>
 <%@ IMPORT NAMESPACE = "System.IO" %>
 <html xmlns="http://www.w3.org/1999/xhtml">
 <script runat ="server">
    String LoadDecodedFile(String file)
       {
       String DecodedString = "";
       FileStream fs = new FileStream(file, FileMode.Open);
       StreamReader r = new StreamReader(fs);
       // Position the file pointer at the beginning of the file.
       r.BaseStream.Seek(0, SeekOrigin.Begin);
       // Read the entire file into a string and decode each chunk. 
       while (r.Peek() > -1)
          DecodedString += Server.HtmlDecode(r.ReadLine());
       r.Close();
       return DecodedString;
       }
 </script>
 <head runat="server">
 <title>HttpServerUtility.HtmlDecode Example</title>
 </head>
 <body></body>
 </html>
728x90
반응형