HttpResponse.Write 方法

定義

將資訊寫入 HTTP 回應輸出資料流。

多載

Write(Char)

將字元寫入 HTTP 回應輸出資料流。

Write(Object)

Object 寫入 HTTP 回應資料流。

Write(String)

將字串寫入 HTTP 回應輸出資料流。

Write(Char[], Int32, Int32)

將字元的陣列寫入 HTTP 回應輸出資料流。

Write(Char)

將字元寫入 HTTP 回應輸出資料流。

C#
public void Write (char ch);

參數

ch
Char

要傳送至 HTTP 輸出資料流的字元。

範例

下列範例會使用 Write 方法建立一系列常數,這些常數會寫入至 ASP.NET 網頁。 程式碼會呼叫這個版本的 Write 方法,以將個別字元常數寫入頁面。

ASP.NET (C#)
    <%

        // 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>

    %>

適用於

.NET Framework 4.8.1 和其他版本
產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Write(Object)

Object 寫入 HTTP 回應資料流。

C#
public void Write (object obj);

參數

obj
Object

要寫入 HTTP 輸出資料流的 Object

適用於

.NET Framework 4.8.1 和其他版本
產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Write(String)

將字串寫入 HTTP 回應輸出資料流。

C#
public void Write (string s);

參數

s
String

要寫入 HTTP 輸出資料流的字串。

範例

下列範例會將用戶端的名稱回應回用戶端的瀏覽器。 方法 HtmlEncode 會移除任何可能已在輸入欄位中提交的 UserName 惡意腳本和無效字元。

C#
Response.Write("Hello " + Server.HtmlEncode(Request.QueryString["UserName"]) + "<br>");
    

備註

如果從 Web 用戶端接收的輸入或從用戶端傳輸回用戶端時,動態產生的 HTML 頁面可能會造成安全性風險。 內嵌在提交至網站的輸入中且稍後寫回給用戶端的惡意腳本,可能似乎源自信任的來源。 此安全性風險稱為跨網站腳本攻擊。 當從您的網站傳輸到用戶端瀏覽器時,您應該一律驗證從用戶端接收的資料。

此外,每當您寫出為 HTML 時,任何收到作為輸入的資料,都應該使用 或 UrlEncode 之類的 HtmlEncode 技術進行編碼,以防止惡意腳本執行。 這項技術適用于未在收到資料時驗證的資料。

當您編碼或篩選資料時,您必須指定網頁的字元集,讓篩選可以識別和移除不屬於該集合的任何位元組序列 (,例如非虛構序列) ,而且可能內嵌惡意腳本。

適用於

.NET Framework 4.8.1 和其他版本
產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1

Write(Char[], Int32, Int32)

將字元的陣列寫入 HTTP 回應輸出資料流。

C#
public void Write (char[] buffer, int index, int count);

參數

buffer
Char[]

要寫入的字元陣列。

index
Int32

開始寫入之字元陣列中的位置。

count
Int32

要寫入的字元數,從 index 開始。

範例

下列範例會使用 Write 方法建立一系列常數,這些常數會寫入至 ASP.NET 網頁。 程式碼會呼叫這個版本的 Write 方法,以將個別字元常數寫入頁面。

ASP.NET (C#)
    <%

        // 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>

    %>

適用於

.NET Framework 4.8.1 和其他版本
產品 版本
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1