HttpResponse.Write 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将信息写入 HTTP 响应输出流。
重载
| 名称 | 说明 |
|---|---|
| Write(Char) |
将字符写入 HTTP 响应输出流。 |
| Write(Object) |
将一个 Object 写入 HTTP 响应流。 |
| Write(String) |
将字符串写入 HTTP 响应输出流。 |
| Write(Char[], Int32, Int32) |
将字符数组写入 HTTP 响应输出流。 |
Write(Char)
将字符写入 HTTP 响应输出流。
public:
void Write(char ch);
public void Write(char ch);
member this.Write : char -> unit
Public Sub Write (ch As Char)
参数
- ch
- Char
要写入 HTTP 输出流的字符。
示例
以下示例使用 Write 方法创建一系列写入 ASP.NET 页的常量。 该代码调用此版本的 Write 方法,以将单个字符常量写入页面。
<%
// Create a character array.
char[] charArray = {'H', 'e', 'l', 'l', 'o', ',', ' ',
'w', 'o', 'r', 'l', 'd'};
// Write a character array to the client.
Response.Write(charArray, 0, charArray.Length);
// Write a single characher.
Response.Write(';');
// Write a sub-section of a character array to the client.
Response.Write(charArray, 0, 5);
// <snippet6>
// Write an object to the client.
object obj = (object)13;
Response.Write(obj);
// </snippet6>
%>
<%
Dim charArray As Char() = {"H"c, "e"c, "l"c, "l"c, "o"c, ","c, " "c, _
"w"c, "o"c, "r"c, "l"c, "d"c}
' Write a character array to the client.
Response.Write(charArray, 0, charArray.Length)
' Write a single character.
Response.Write(";"c)
' Write a sub-section of a character array to the client.
Response.Write(charArray, 0, 5)
' <snippet6>
' Write an object to the client.
Dim obj As Object
obj = CType(13, Object)
Response.Write(obj)
' </snippet6>
%>
适用于
Write(String)
将字符串写入 HTTP 响应输出流。
public:
void Write(System::String ^ s);
public void Write(string s);
member this.Write : string -> unit
Public Sub Write (s As String)
参数
- s
- String
要写入 HTTP 输出流的字符串。
示例
以下示例将客户端的名称回显回客户端的浏览器。 该方法 HtmlEncode 将去除任何可能已在输入字段中提交的 UserName 恶意脚本和无效字符。
Response.Write("Hello " + Server.HtmlEncode(Request.QueryString["UserName"]) + "<br>");
Response.Write("Hello " & Server.HtmlEncode(Request.QueryString("UserName")) & "<br>")
注解
如果从 Web 客户端接收的输入或从客户端传输回客户端时,动态生成的 HTML 页可能会带来安全风险。 嵌入到提交到网站的输入中的恶意脚本,稍后写回客户端可能似乎是源自受信任的源。 此安全风险称为跨站点脚本攻击。 在从站点传输到客户端浏览器时,应始终验证从客户端接收的数据。
此外,每当以 HTML 形式写出作为输入接收的任何数据时,都应使用诸如 HtmlEncode 或 UrlEncode 防止恶意脚本执行等技术对其进行编码。 此方法对收到数据时未验证的数据非常有用。
对数据进行编码或筛选时,必须为网页指定字符集,以便筛选器可以识别和删除不属于该集的任何字节序列(例如非幻象序列),并可能在其中嵌入恶意脚本。
适用于
Write(Char[], Int32, Int32)
将字符数组写入 HTTP 响应输出流。
public:
void Write(cli::array <char> ^ buffer, int index, int count);
public void Write(char[] buffer, int index, int count);
member this.Write : char[] * int * int -> unit
Public Sub Write (buffer As Char(), index As Integer, count As Integer)
参数
- buffer
- Char[]
要写入的字符数组。
- index
- Int32
写入开始的字符数组中的位置。
- count
- Int32
要写入的字符数,从开头 index。
示例
以下示例使用 Write 方法创建一系列写入 ASP.NET 页的常量。 该代码调用此版本的 Write 方法,以将单个字符常量写入页面。
<%
// Create a character array.
char[] charArray = {'H', 'e', 'l', 'l', 'o', ',', ' ',
'w', 'o', 'r', 'l', 'd'};
// Write a character array to the client.
Response.Write(charArray, 0, charArray.Length);
// Write a single characher.
Response.Write(';');
// Write a sub-section of a character array to the client.
Response.Write(charArray, 0, 5);
// <snippet6>
// Write an object to the client.
object obj = (object)13;
Response.Write(obj);
// </snippet6>
%>
<%
Dim charArray As Char() = {"H"c, "e"c, "l"c, "l"c, "o"c, ","c, " "c, _
"w"c, "o"c, "r"c, "l"c, "d"c}
' Write a character array to the client.
Response.Write(charArray, 0, charArray.Length)
' Write a single character.
Response.Write(";"c)
' Write a sub-section of a character array to the client.
Response.Write(charArray, 0, 5)
' <snippet6>
' Write an object to the client.
Dim obj As Object
obj = CType(13, Object)
Response.Write(obj)
' </snippet6>
%>