HttpUtility.HtmlDecode 方法
定义
将已经为 HTTP 传输进行过 HTML 编码的字符串转换为已解码的字符串。Converts a string that has been HTML-encoded for HTTP transmission into a decoded string.
若要对 Web 应用程序之外的值进行编码或解码,请使用 WebUtility 类。To encode or decode values outside of a web application, use the WebUtility class.
重载
| HtmlDecode(String) |
将已经为 HTTP 传输进行过 HTML 编码的字符串转换为已解码的字符串。Converts a string that has been HTML-encoded for HTTP transmission into a decoded string. |
| HtmlDecode(String, TextWriter) |
将已经过 HTML 编码的字符串转换为已解码的字符串并将其发送给 TextWriter 输出流。Converts a string that has been HTML-encoded into a decoded string, and sends the decoded string to a TextWriter output stream. |
HtmlDecode(String)
将已经为 HTTP 传输进行过 HTML 编码的字符串转换为已解码的字符串。Converts a string that has been HTML-encoded for HTTP transmission into a decoded string.
public:
static System::String ^ HtmlDecode(System::String ^ s);
public static string? HtmlDecode (string? s);
public static string HtmlDecode (string s);
static member HtmlDecode : string -> string
Public Shared Function HtmlDecode (s As String) As String
参数
- s
- String
要解码的字符串。The string to decode.
返回
已解码的字符串。A decoded string.
示例
下面的代码示例演示 HtmlEncode 类的和 HtmlDecode 方法 HttpUtility 。The following code example demonstrates the HtmlEncode and HtmlDecode methods of the HttpUtility class. 使用方法对输入字符串进行编码 HtmlEncode 。The input string is encoded using the HtmlEncode method. 然后,使用方法对获取的编码字符串进行解码 HtmlDecode 。The encoded string obtained is then decoded using the HtmlDecode method.
using System;
using System.Web;
using System.IO;
class MyNewClass
{
public static void Main()
{
Console.WriteLine("Enter a string having '&', '<', '>' or '\"' in it: ");
string myString = Console.ReadLine();
// Encode the string.
string myEncodedString = HttpUtility.HtmlEncode(myString);
Console.WriteLine($"HTML Encoded string is: {myEncodedString}");
StringWriter myWriter = new StringWriter();
// Decode the encoded string.
HttpUtility.HtmlDecode(myEncodedString, myWriter);
string myDecodedString = myWriter.ToString();
Console.Write($"Decoded string of the above encoded string is: {myDecodedString}");
}
}
Imports System.Web
Imports System.IO
Class MyNewClass
Public Shared Sub Main()
Dim myString As String
Console.WriteLine("Enter a string having '&' or '""' in it: ")
myString = Console.ReadLine()
Dim myEncodedString As String
' Encode the string.
myEncodedString = HttpUtility.HtmlEncode(myString)
Console.WriteLine("HTML Encoded string is " + myEncodedString)
Dim myWriter As New StringWriter()
' Decode the encoded string.
HttpUtility.HtmlDecode(myEncodedString, myWriter)
Console.Write("Decoded string of the above encoded string is " + myWriter.ToString())
End Sub
End Class
注解
如果传入 HTTP 流中的字符(如空格和标点符号),则可能会在接收端对其进行错误解释。If characters such as blanks and punctuation are passed in an HTTP stream, they might be misinterpreted at the receiving end. HTML 编码将 HTML 中不允许的字符转换为字符实体等效项;HTML 解码反转编码。HTML encoding converts characters that are not allowed in HTML into character-entity equivalents; HTML decoding reverses the encoding. 例如,嵌入到文本块中时,会将字符 < and > 编码为,并将其编码为 < > HTTP 传输。For example, when embedded in a block of text, the characters < and > are encoded as < and > for HTTP transmission.
若要对 Web 应用程序之外的值进行编码或解码,请使用 WebUtility 类。To encode or decode values outside of a web application, use the WebUtility class.
另请参阅
适用于
HtmlDecode(String, TextWriter)
将已经过 HTML 编码的字符串转换为已解码的字符串并将其发送给 TextWriter 输出流。Converts a string that has been HTML-encoded into a decoded string, and sends the decoded string to a TextWriter output stream.
public:
static void HtmlDecode(System::String ^ s, System::IO::TextWriter ^ output);
public static void HtmlDecode (string? s, System.IO.TextWriter output);
public static void HtmlDecode (string s, System.IO.TextWriter output);
static member HtmlDecode : string * System.IO.TextWriter -> unit
Public Shared Sub HtmlDecode (s As String, output As TextWriter)
参数
- s
- String
要解码的字符串。The string to decode.
- output
- TextWriter
TextWriter 输出流。A TextWriter stream of output.
注解
如果传入 HTTP 流中的字符(如空格和标点符号),则可能会在接收端对其进行错误解释。If characters such as blanks and punctuation are passed in an HTTP stream, they might be misinterpreted at the receiving end. HTML 编码将 HTML 中不允许的字符转换为字符实体等效项;HTML 解码反转编码。HTML encoding converts characters that are not allowed in HTML into character-entity equivalents; HTML decoding reverses the encoding. 例如,嵌入到文本块中时,会将字符 < and > 编码为,并将其编码为 < > HTTP 传输。For example, when embedded in a block of text, the characters < and > are encoded as < and > for HTTP transmission.
若要对 Web 应用程序之外的值进行编码或解码,请使用 WebUtility 类。To encode or decode values outside of a web application, use the WebUtility class.