C# - The HttpResponse Class

The HttpResponse class is under System.Web namespace. It contains methods and properties necessary to crafting an HTTP response. Several methods are used to modify headers, cookies, and the actual content of the response.

Properties and Methods of the HttpResponse Class

Item Description
Properties
Buffer If set to True, the entire response is buffered in memory until the response is finished before sending to client
BufferOutput If set to True, the entire response is buffered in memory until the response is finished before sending to client
CacheControl Sets the response cache-control header to Public or Private
Charset Contains the HTTP character set for the response
ContentEncoding Contains the HTTP character set for the response
ContentType Contains the HTTP MIME type of the response
Cookies Contains the response cookie collection (that is, the cookies that will be transmitted back to the client on the current response)
Expires Contains the number of minutes before the page transmitted to the client expires
ExpiresAbsolute Contains the absolute date and time for a page to be removed from the cache
IsClientConnected Contains True if the client is still connected to the Web server
Status Contains the HTTP status sent to the client
StatusCode Contains the integer value of the HTTP status sent to the client
StatusDescription Contains the HTTP status description sent to the client
SuppressContent Contains True if HTML content should not be sent to the client
Methods
AddFileDependency() Adds a single file to the collection of files that the current response depends on
AddHeader() Adds another header to the header collection
AppendCookie() Adds a cookie to the cookie collection
AppendHeader() Adds another header to the header collection
AppendToLog() Adds log information to the IIS log file
BinaryWrite() Writes a string of binary characters to the output stream
Clear() Clears the buffer stream for the current response
ClearContent() Clears the buffer stream for the current response
ClearHeaders() Clears all headers from the output stream
Close() Closes the connection to the client
End() Sends all buffered output to the client and ceases execution of the current response
Flush() Sends all buffered output to the client
Pics() Adds a Platform for Internet Content Selection (PICS)-Label header to the header collection
Redirect() Redirects a client to a new URL
Write() Writes information to the output stream
WriteFile() Writes a file directly to the output stream
HttpResponse.Buffer

Syntax

Boolean Buffer

Description

The Buffer property enables a developer to specify that the entire response should be held in memory until processing is finished and then sent to the client.

Example

Response.Buffer = true;
HttpResponse.BufferOutput

Syntax

Boolean BufferOutput

Description

The BufferOutput property enables a developer to specify that the entire response should be held in memory until processing is finished and then sent to the client.

Example

Response.BufferOutput = true;
HttpResponse.CacheControl

Syntax

String CacheControl

Description

The CacheControl property is used to set the cache-control HTTP header to Public or Private

Example

Response.CacheControl = "Public";
HttpResponse.Charset

Syntax

String Charset

Description

The Charset property contains the charset of the response.

Example

string charSet = Response.Charset;
HttpResponse.ContentEncoding

Syntax

Encoding ContentEncoding

Description

The ContentEncoding property is an encoding object that contains information about the character set of the current response.

Example

string charSetName = Response.ContentEncoding.EncodingName;
HttpResponse.ContentType

Syntax

String ContentType

Description

The ContentType property contains the HTTP MIME type of the current response.

Example

string contentType = Resposen.ContentType;
HttpResponse.Cookies

Syntax

HttpCookieCollection Cookies

Description

The Cookies property contains a collection of all cookies to be transmitted to the client in the current response.

Example

HttpCookie newCookie = new HttpCookie("FullName");
newCookie.Value = "TriConsole";
Response.Cookies.Add(newCookie);
HttpResponse.Expires

Syntax

Int32 Expires

Description

The Expires property contains the amount of time, in minutes, before the response expires.

Example

Response.Expires = 60;
HttpResponse.ExpiresAbsolute

Syntax

DateTime ExpiresAbsolute

Description

The ExpiresAbsolute property specifies the date and time at which a page will expire.

Example

Response.ExpiresAbsolute = DateTime.Now;
HttpResponse.IsClientConnected

Syntax

Boolean IsClientConnected

Description

The IsClientConnected property contains True if the client is still connected to the Web server during the processing of a response.

Example

if(Response.IsClientConnected){
    //process the query since we know client is connected
}
HttpResponse.Status

Syntax

String Status

Description

The Status property contains the HTTP status that is sent to the client.

Example

string status = Response.Status;
HttpResponse.StatusCode

Syntax

Int32 StatusCode

Description

The StatusCode property contains the HTTP status code sent to the client.

Example

int statusCode = Response.StatusCode;
HttpResponse.StatusDescription

Syntax

String StatusDescription

Description

The StatusDescription property contains the description of the HTTP status sent to the client.

Example

string statusDescription = Response.StatusDescription;
HttpResponse.SuppressContent

Syntax

Boolean SuppressContent

Description

If the SuppressContent property contains True, HTTP output will not be sent to the client.

Example

Response.SuppressContent = true;
HttpResponse.AddHeader()

Syntax

void AddHeader(String name, String value)

Description

The AddHeader() method adds a new header to the current response.

Example

Response.AddHeader("NewHeader", "NewHeaderValue");
HttpResponse.AppendCookie()

Syntax

void AppendCookie(HttpCookie cookie)

Description

The AppendCookie() method adds a cookie to the cookie collection.

Example

HttpCookie newCookie = new HttpCookie("UserName");
newCookie.Value = "triconsole";
Response.AppendCookie("newCookie");
HttpResponse.AppendHeader()

Syntax

void AppendHeader()

Description

The AppendHeader() method adds a new header to the collection of headers sent to the client. An exception error occurs if this method is used after the headers have already been sent. Because the entire HTTP response is buffered until all server-side processing is complete, this exception occurs only if the default behavior is changed.

Example

Response.AppendHeader("NewHeader", "NewHeaderValue");
HttpResponse.AppendToLog()

Syntax

void AppendToLog(String param)

Description

The AppendToLog() method adds a custom entry to the IIS log.

Example

Response.AppendToLog("Custom Log Entry Information");
HttpResponse.BinaryWrite()

Syntax

void BinaryWrite(Byte[] buffer)

Description

The BinaryWrite() method writes a string of binary characters to the HTTP output stream.

Example

FileStream fs = new FileStream("c:\\example.txt", FileMode.Open);
long FileSize = fs.Length;


byte[] Buffer = new byte[(int)FileSize];
fs.Read(Buffer, 0, (int)FileSize);
fs.Close();


Response.Write("Contents of File: ");
Response.BinaryWrite(Buffer);
HttpResponse.Clear()

Syntax

void Clear()

Description

The Clear() method removes all content from the response buffer.

Example

Response.Clear();
HttpResponse.ClearContent()

Syntax

void ClearContent()

Description

The ClearContent() method removes all content from the response buffer.

Example

Response.ClearContent();
HttpResponse.ClearHeaders()

Syntax

void ClearHeaders()

Description

The ClearHeaders() method removes all headers from the response buffer.

Example

Response.ClearHeaders();
HttpResponse.Close()

Syntax

void Close()

Description

The Close() method closes the connection to the client.

Example

Response.Close();
HttpResponse.End()

Syntax

void End()

Description

The End() method flushes the response buffer and then closes the connection to the client.

Example

Response.End();
HttpResponse.Flush()

Syntax

void Flush()

Description

The Flush() method sends all information that is currently in the response buffer to the client.

Example

Response.Flush();
HttpResponse.Pics()

Syntax

void Pics(String value)

Description

The Pics() method adds a PICS-Label header to the current response.

Example

Response.Pics("All-Ages");
HttpResponse.Redirect()

Syntax

void Redirect(String url)

Description

The Redirect() method sends the client to a new URL. Because the Redirect() method actually preforms the redirection through a META tag, if the headers have already been sent to the client, an exception error is generated.

Example

Response.Redirect("http://www.yahoo.com");
HttpResponse.Write()

Syntax

void Write(Char[] buffer, Int32 index, Int32 count)
void Write(Char ch)
void Write(Object obj)
void Write(String s)

Description

The Write() method writes information to the output stream for the current request.

Example

Response.Write("Hello test!!");
HttpResponse.WriteFile()

Syntax

void WriteFile(IntPtr fileHandle, Int64 offset, Int64 size)
void WriteFile(String fileName)
void WriteFile(String fileName, Boolean readIntoMemory)
void WriteFile(String fileName, Int64 offset, Int64 size)

Description

The WriteFile() method writes the contents of a file directory to the output stream for a response.

Example

Response.WriteFile("c:\\example.txt");
Write a review
Message:
Rating:
Name:
Email:
(optional)
(Your email that entered here will not show anywhere on website and will use only reference when someone reply your post)
  I want to receive a copy of email on this post (your email is needed)
 
Code: