HttpResponse 類別

定義

封裝來自 ASP.NET 作業的 HTTP 回應資訊。

public ref class HttpResponse sealed
public sealed class HttpResponse
type HttpResponse = class
Public NotInheritable Class HttpResponse
繼承
HttpResponse

範例

下列範例會在要求頁面時繪製三個重迭矩形。 程式碼會從將 ContentType 屬性設定為 image/jpeg 開始,讓整個頁面轉譯為 JPEG 影像。 然後,程式碼會呼叫 Clear 方法,以確保不會使用此回應傳送任何多餘的內容。 接下來,程式碼會將 BufferOutput 屬性設定為 true,以便頁面在傳送至要求用戶端之前完全處理。 接著會建立用來繪製矩形的兩個 Graphics 物件:和 Bitmap 物件。 在頁面中建立的變數會當做座標來繪製矩形,以及出現在最大矩形內的字串。

當繪製三個矩形及其內出現的字串時,會將 Bitmap 儲存到 Stream 與 屬性相關聯的 OutputStream 物件,且其格式設定為 JPEG。 程式碼會呼叫 DisposeDispose 方法來釋放兩個繪圖物件所使用的資源。 最後,程式碼會呼叫 方法, Flush 以將緩衝回應傳送至要求用戶端。

注意

在程式碼中 HttpResponse ,物件是由 關鍵字 Response 所參考。 例如, Response.Clear() 參考 HttpResponse.Clear 方法。 類別 Page 具有名為 Response 的屬性,該屬性會公開 的目前實例 HttpResponse

<%@ Page Language="C#" %>
<%@ import Namespace="System.Drawing" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<%@ import Namespace="System.Drawing.Drawing2D" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

    private void Page_Load(object sender, EventArgs e)
    {
// <snippet2>
        // Set the page's content type to JPEG files
        // and clears all content output from the buffer stream.
        Response.ContentType = "image/jpeg";
        Response.Clear();
    
        // Buffer response so that page is sent
        // after processing is complete.
        Response.BufferOutput = true;
// </snippet2>
    
        // Create a font style.
        Font rectangleFont = new Font(
            "Arial", 10, FontStyle.Bold);
    
        // Create integer variables.
        int height = 100;
        int width = 200;
    
        // Create a random number generator and create
        // variable values based on it.
        Random r = new Random();
        int x = r.Next(75);
        int a = r.Next(155);
        int x1 = r.Next(100);
    
        // Create a bitmap and use it to create a
        // Graphics object.
        Bitmap bmp = new Bitmap(
            width, height, PixelFormat.Format24bppRgb);
        Graphics g = Graphics.FromImage(bmp);
    
        g.SmoothingMode = SmoothingMode.AntiAlias;
        g.Clear(Color.LightGray);
    
        // Use the Graphics object to draw three rectangles.
        g.DrawRectangle(Pens.White, 1, 1, width-3, height-3);
        g.DrawRectangle(Pens.Aquamarine, 2, 2, width-3, height-3);
        g.DrawRectangle(Pens.Black, 0, 0, width, height);
    
        // Use the Graphics object to write a string
        // on the rectangles.
        g.DrawString(
            "ASP.NET Samples", rectangleFont,
            SystemBrushes.WindowText, new PointF(10, 40));
    
        // Apply color to two of the rectangles.
        g.FillRectangle(
            new SolidBrush(
                Color.FromArgb(a, 255, 128, 255)),
            x, 20, 100, 50);
    
        g.FillRectangle(
            new LinearGradientBrush(
                new Point(x, 10),
                new Point(x1 + 75, 50 + 30),
                Color.FromArgb(128, 0, 0, 128),
                Color.FromArgb(255, 255, 255, 240)),
            x1, 50, 75, 30);

// <snippet3>    
        // Save the bitmap to the response stream and
        // convert it to JPEG format.
        bmp.Save(Response.OutputStream, ImageFormat.Jpeg);
    
        // Release memory used by the Graphics object
        // and the bitmap.
        g.Dispose();
        bmp.Dispose();
    
        // Send the output to the client.
        Response.Flush();
// </snippet3>
    }

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
    </form>
</body>
</html>
<%@ Page Language="VB" %>
<%@ import Namespace="System.Drawing" %>
<%@ import Namespace="System.Drawing.Imaging" %>
<%@ import Namespace="System.Drawing.Drawing2D" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">

   Private Sub Page_Load(sender As Object, e As EventArgs)
' <snippet2>
      ' Set the page's content type to JPEG files
      ' and clears all content output from the buffer stream.
      Response.ContentType = "image/jpeg"
      Response.Clear()
      
      ' Buffer response so that page is sent
      ' after processing is complete.
      Response.BufferOutput = True
' </snippet2>
      
      ' Create a font style.
      Dim rectangleFont As New Font( _
          "Arial", 10, FontStyle.Bold)
      
      ' Create integer variables.
      Dim height As Integer = 100
      Dim width As Integer = 200
      
      ' Create a random number generator and create
      ' variable values based on it.
      Dim r As New Random()
      Dim x As Integer = r.Next(75)
      Dim a As Integer = r.Next(155)
      Dim x1 As Integer = r.Next(100)
      
      ' Create a bitmap and use it to create a
      ' Graphics object.
      Dim bmp As New Bitmap( _
          width, height, PixelFormat.Format24bppRgb)
      Dim g As Graphics = Graphics.FromImage(bmp)
      
      g.SmoothingMode = SmoothingMode.AntiAlias
      g.Clear(Color.LightGray)
      
      ' Use the Graphics object to draw three rectangles.
      g.DrawRectangle(Pens.White, 1, 1, width - 3, height - 3)
      g.DrawRectangle(Pens.Aquamarine, 2, 2, width - 3, height - 3)
      g.DrawRectangle(Pens.Black, 0, 0, width, height)
      
      ' Use the Graphics object to write a string
      ' on the rectangles.
      g.DrawString("ASP.NET Samples", rectangleFont, SystemBrushes.WindowText, New PointF(10, 40))
      
      ' Apply color to two of the rectangles.
      g.FillRectangle( _
          New SolidBrush( _
              Color.FromArgb(a, 255, 128, 255)), _
          x, 20, 100, 50)
      
      g.FillRectangle( _
          New LinearGradientBrush( _
              New Point(x, 10), _
              New Point(x1 + 75, 50 + 30), _
              Color.FromArgb(128, 0, 0, 128), _
              Color.FromArgb(255, 255, 255, 240)), _
          x1, 50, 75, 30)

' <snippet3>      
      ' Save the bitmap to the response stream and
      ' convert it to JPEG format.
      bmp.Save(Response.OutputStream, ImageFormat.Jpeg)
      
      ' Release memory used by the Graphics object
      ' and the bitmap.
      g.Dispose()
      bmp.Dispose()
      
      ' Send the output to the client.
      Response.Flush()
' </snippet3>
   End Sub 'Page_Load

</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>ASP.NET Example</title>
</head>
<body>
    <form id="form1" runat="server">
    </form>
</body>
</html>

備註

類別的方法和屬性會透過 ResponseHttpContextPageUserControl 類別的 HttpResponseHttpApplication 屬性公開。

類別的 HttpResponse 下列方法僅在回傳案例中受到支援,而不是在非同步回傳案例中支援:

當您使用 UpdatePanel 控制項來更新頁面的選取區域,而不是使用回傳來更新整個頁面時,就會啟用部分頁面更新。 如需詳細資訊,請參閱 UpdatePanel 控制項概觀部分頁面轉譯概觀

建構函式

HttpResponse(TextWriter)

初始化 HttpResponse 類別的新執行個體。

屬性

Buffer

取得或設定值,指出是否要緩衝輸出並在整個回應處理完成之後將它送出。

BufferOutput

取得或設定值,指出是否要緩衝輸出並在整個網頁處理完成之後送出。

Cache

取得網頁的快取原則 (例如,到期時間、隱私權設定與 Vary 子句)。

CacheControl

取得或設定符合其中一個 Cache-Control 列舉值的 HttpCacheability HTTP 標頭。

Charset

取得或設定輸出資料流的 HTTP 字元集。

ClientDisconnectedToken

取得用戶端中斷連接時發生錯誤的 CancellationToken 物件。

ContentEncoding

取得或設定輸出資料流的 HTTP 字元集。

ContentType

取得或設定輸出資料流的 HTTP MIME 類型。

Cookies

取得回應 Cookie 集合。

Expires

取得或設定瀏覽器上快取網頁到期以前的分鐘數。 如果使用者在頁面到期前返回相同的頁面,則會顯示快取版本。 提供 Expires 的目的,是為了與 ASP 的舊版本相容。

ExpiresAbsolute

取得或設定從快取中移除快取資訊的絕對日期和時間。 提供 ExpiresAbsolute 的目的,是為了與 ASP 的舊版本相容。

Filter

取得或設定用來在傳輸之前修改 HTTP 實體主體的包裝篩選物件。

HeaderEncoding

取得或設定 Encoding 物件,表示目前標頭輸出資料流的編碼方式。

Headers

取得回應標頭的集合。

HeadersWritten

取得指出是否已寫入回應標頭的值。

IsClientConnected

取得值,指出用戶端是否仍然與伺服器連接著。

IsRequestBeingRedirected

取得布林值,指出用戶端是否傳輸至新位置。

Output

允許文字輸出至傳出 HTTP 回應資料流。

OutputStream

允許二進位輸出至傳出 HTTP 內容主體。

RedirectLocation

取得或設定 Http Location 標頭的值。

Status

設定傳回至用戶端的 Status 行。

StatusCode

取得或設定傳回至用戶端的輸出之 HTTP 狀態碼。

StatusDescription

取得或設定傳回至用戶端的輸出之 HTTP 狀態字串。

SubStatusCode

取得或設定的值會評估回應的狀態碼是否合格。

SupportsAsyncFlush

取得值,指出連接是否支援非同步清除作業。

SuppressContent

取得或設定值,指出是否送出 HTTP 內容至用戶端。

SuppressDefaultCacheControlHeader

取得或設定值,指出是否要隱藏目前 HTTP 回應的預設 Cache Control: private 標頭。

SuppressFormsAuthenticationRedirect

取得或設定值,指定是否應該抑制表單驗證重新導向至登入頁面。

TrySkipIisCustomErrors

取得或設定值,指定是否停用 IIS 7.0 自訂錯誤。

方法

AddCacheDependency(CacheDependency[])

如果回應儲存在輸出快取中而且指定的相依性變更,便使快取相依性集合與回應產生關聯,加速回應失效。

AddCacheItemDependencies(ArrayList)

讓快取回應的有效性取決於快取中的其他項目。

AddCacheItemDependencies(String[])

讓快取項目的有效性取決於快取中的另一個項目。

AddCacheItemDependency(String)

讓快取回應的有效性取決於快取中的另一個項目。

AddFileDependencies(ArrayList)

將檔案名稱群組加入目前回應所依存的檔案名稱的集合。

AddFileDependencies(String[])

將檔案名稱陣列加入目前回應所依存的檔案名稱的集合。

AddFileDependency(String)

將單一檔案名稱加入目前回應所依存的檔案名稱的集合。

AddHeader(String, String)

將 HTTP 標頭加入輸出資料流。 提供 AddHeader(String, String) 的目的,是為了與 ASP 的舊版本相容。

AddOnSendingHeaders(Action<HttpContext>)

註冊 ASP.NET 執行階段會立即在回應標頭傳送給這個要求之前叫用的回呼。

AppendCookie(HttpCookie)

將 HTTP Cookie 加入內建 Cookie 集合中。

AppendHeader(String, String)

將 HTTP 標頭加入輸出資料流。

AppendToLog(String)

將自訂記錄資訊加入至網際網路資訊服務 (IIS) 記錄檔。

ApplyAppPathModifier(String)

如果工作階段使用 Cookieless 工作階段狀態並且傳回合併的路徑,則將工作階段 ID 加入虛擬路徑。 如果沒有使用 Cookieless 工作階段狀態,則 ApplyAppPathModifier(String) 會傳回原始虛擬路徑。

BeginFlush(AsyncCallback, Object)

將目前已緩衝的回應傳送到用戶端。

BinaryWrite(Byte[])

將二進位字元的字串寫入 HTTP 輸出資料流。

Clear()

從緩衝區資料流清除所有內容輸出。

ClearContent()

從緩衝區資料流清除所有內容輸出。

ClearHeaders()

從緩衝區資料流清除所有標頭。

Close()

關閉與用戶端的通訊端連接。

DisableKernelCache()

停用目前回應的核心快取處理。

DisableUserCache()

停用這個回應的 IIS 使用者模式快取。

End()

將目前所有受緩衝的輸出傳送到用戶端、停止網頁的執行,並引發 EndRequest 事件。

EndFlush(IAsyncResult)

完成非同步的清除作業。

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
Flush()

送出所有目前正在緩衝的輸出到用戶端。

FlushAsync()

以非同步方式將目前所緩衝的所輸出輸送到用戶端。

GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
Pics(String)

附加 HTTP PICS-Label 標頭至輸出資料流。

PushPromise(String)

支援傳送推送 Promise 給 HTTP 2.0 用戶端的應用程式。 如需詳細資訊,請參閱 HTTP/2 Specification Section 8.2:Server Push (HTTP/2 規格第 8.2 節:伺服器推送。

PushPromise(String, String, NameValueCollection)

支援傳送推送 Promise 給 HTTP 2.0 用戶端的應用程式。 如需詳細資訊,請參閱 HTTP/2 Specification Section 8.2:Server Push (HTTP/2 規格第 8.2 節:伺服器推送。

Redirect(String)

重新導向要求至新的 URL 並指定新的 URL。

Redirect(String, Boolean)

重新導向用戶端至新的 URL。 指定新 URL 和是否應該結束目前網頁的執行。

RedirectPermanent(String)

執行從要求之 URL 至指定之 URL 的永久重新導向。

RedirectPermanent(String, Boolean)

執行從要求之 URL 至指定之 URL 的永久重新導向,並提供完成回應的選項。

RedirectToRoute(Object)

使用路由參數值,將要求重新導向至新的 URL。

RedirectToRoute(RouteValueDictionary)

使用路由參數值,將要求重新導向至新的 URL。

RedirectToRoute(String)

使用路由名稱,將要求重新導向至新的 URL。

RedirectToRoute(String, Object)

使用路由參數值及路由名稱,將要求重新導向至新的 URL。

RedirectToRoute(String, RouteValueDictionary)

使用路由參數值及路由名稱,將要求重新導向至新的 URL。

RedirectToRoutePermanent(Object)

使用路由參數值,執行永遠從要求的 URL 重新導向至指定的 URL。

RedirectToRoutePermanent(RouteValueDictionary)

使用路由參數值,執行永遠從要求的 URL 重新導向至指定的 URL。

RedirectToRoutePermanent(String)

使用路由名稱,執行永遠從要求的 URL 重新導向至指定的 URL。

RedirectToRoutePermanent(String, Object)

使用路由參數值以及對應到新 URL 的路由名稱,執行永遠從要求的 URL 重新導向至指定的 URL。

RedirectToRoutePermanent(String, RouteValueDictionary)

使用路由參數值及路由名稱,執行永遠從要求的 URL 重新導向至指定的 URL。

RemoveOutputCacheItem(String)

從快取移除與預設輸出快取提供者關聯的所有快取項目。 此方法為靜態。

RemoveOutputCacheItem(String, String)

使用指定的輸出快取提供者,移除所有與指定之路徑關聯的輸出快取項目。

SetCookie(HttpCookie)

因為 HttpResponse.SetCookie 方法僅供內部使用,所以您不應該在程式碼中呼叫它。 相反地,您可以呼叫 HttpResponse.Cookies.Set 方法,如下列範例所示。
更新 Cookie 集合中的現有 Cookie。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)
TransmitFile(String)

將指定的檔案直接寫入 HTTP 回應輸出資料流,而不在記憶體中緩衝。

TransmitFile(String, Int64, Int64)

將指定的檔案部分直接寫入 HTTP 回應輸出資料流,而不在記憶體中緩衝。

Write(Char)

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

Write(Char[], Int32, Int32)

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

Write(Object)

Object 寫入 HTTP 回應資料流。

Write(String)

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

WriteFile(IntPtr, Int64, Int64)

將指定檔案直接寫入 HTTP 回應輸出資料流。

WriteFile(String)

將指定的檔案內容直接寫入 HTTP 回應輸出資料流,做為檔案區塊。

WriteFile(String, Boolean)

將指定的檔案內容直接寫入 HTTP 回應輸出資料流,做為記憶體區塊。

WriteFile(String, Int64, Int64)

將指定檔案直接寫入 HTTP 回應輸出資料流。

WriteSubstitution(HttpResponseSubstitutionCallback)

允許回應替代區塊插入回應,以便為輸出快取的回應,動態產生指定的回應區域。

適用於