Http Response Object Methods

In addition to the Write method, which you've already seen, the HttpResponse object contains several other useful methods. (I've omitted the methods that the HttpResponse object inherits from Object, because they're common to all objects.)

AddFileDependencies A response may depend on the existence and availability of one or more files. If the files are not available for any reason, the Response object raises an error. You can add files to the dependency list one at a time with the

AddFileDependency method. The AddFileDependencies method lets you add several files at once by passing the method an ArrayList containing a list of filenames.

Array fList = new Array("file1.txt", "file2.txt"); Response.AddFileDependencies(fList);

AddFileDependency A response may depend on the existence and availability of one or more files. If the files are not available for any reason, the Response object raises an error. The AddFile-Dependency method adds a single file to the dependency list.

AppendHeader This method appends an HTTP header to the response. You must append new headers before writing any content; otherwise the Response object raises an error. For example, you can write a cookie using the AppendHeader method as well as with the Response.Cookies collection. The method accepts two strings; the first string is the header name, and the second string is the header value. To write a cookie with the AppendHeader method, you would write the following:

Response.AppendHeader("Set-Cookie", "myCookie=someCookieValue");

AppendToLog The AppendToLog method lets you write information to the IIS log. For example, the line Response.AppendToLog("This is a test"); causes IIS to write a line in the log like this (the entry appears in the log as a single line):

2001-04-05 04:45:09 - 80 GET /CSharpASP/ch6/ch6-11.aspx This+is+a+test. 200 Mozilla/4.0+(compatible;+MSIE+5.5;+windows

+NT+5.0+COMM++1.0.22 04 to the IIS log. By default, you can find the IIS log in your %System%\Logfiles\W3SVC1 folder. See the example code for Form ch6-11.aspx for a more complete example.

BinaryWrite You use the BinaryWrite method to write raw byte data to the browser. In classic ASP, this was the only method you could use to write binary data from a file. Other new methods, such as WriteFile, have made the BinaryWrite method less common in Web applications, but you can still use it to write raw file data. To try an example, open Notepad, enter some text in the default file, and save it as c:\junk.txt. Next, create a new Web Form called ch6-12.aspx and enter the code from Listing 6.11 in the code-behind class in the Load event. When you run the code, you will see the content you entered into the text file.

Listing 6.11: Example of the Response.BinaryWrite Method (ch6-12.aspx.cs)

FileStream fs = File.OpenRead("c:\\junk.txt"); BinaryReader br = new BinaryReader(fs); int size = 0;

size = (int) br.BaseStream.Length;

Response.Write("<pre>"); Response.BinaryWrite(b); Response.Write("</pre>"); Response.End();

Response.AddHeader("set-cookie", "me=me"); Response.Write("Cookie written");*/ Response.Write("<pre>"); Response.WriteFile("c:\\junk.txt"); Response.Write("</pre>");

Clear This method removes all content from the response buffer. The Clear method does not prevent content you may already have sent to the client (using the Flush method) from rendering, and does not stop the remainder of the page from executing, so the page may still send data to the browser after using the Clear method.

ClearHeaders ClearHeaders removes any existing header content from the response buffer. Using this method does not raise an error if you have already sent content to the client, but neither does it prevent the client from processing any headers already sent.

Close This closes the socket connection, but it doesn't terminate the response. For example, you might call the Close method for a response that initiates a long database operation. In that situation, you would respond to the client, flush the response, and then close the connection. After closing the connection, your application is unable to send any further response data to the client, but it can still perform processing initiated by the request—for example, updating a database, parsing files, or calculating report values. You might also use the Close method to ensure that buffered data is not sent (note that you can use the Clear and ClearHeaders methods instead). The End method sends buffered output to the client before terminating the response, so if you want to avoid that, you might call the Close method before calling the End method.

End End sends any currently buffered content to the client and terminates the response immediately, closing the socket connection. Any code farther down the execution sequence in the same code-behind page that might otherwise have executed does not execute; however, event code in the global.asax file (such as

Application_EndRequest) still executes.

Flush Sends any buffered data to the client. For example, if you're building a large page, you can flush the data periodically so that the client can begin displaying the data before the response is complete. This speeds up the apparent time required to display the page, even though it slightly increases the actual time required.

Pics Internet pages can supply a Platform for Internet Content Selection (PICS) header that clients use to determine the appropriateness of the content. The header is commonly known as a PICS label. A PICS label is nothing but a metatag containing the name pics-label and a value supplied by one of a number of label bureaus (sometimes called rating bureaus). You can, of course, simply include the label in the HTML file for each Web Form, but the Pics method makes it easy to build dynamic pages that include the ratings. For more information see /.

Redirect The Redirect method causes the client to rerequest the page or request another page. It takes one parameter—the URL of the page the client should request. It works by placing a redirect header in the response. For example, the header <meta HTTP-EQUiV="refresh" content="3"> refreshes the current page after 3 seconds. The refresh directive tells the client to request a page, and the content contains the number of seconds the client should wait before making the request. By adding a URL in the content, you can have the client request a different page. For example, the following line causes the client to request Microsoft's default page after 3 seconds:

<META HTTP-EQUIV="refresh" content="3; URL=">

Writing the full HTML header using the Response.AddHeader method is more powerful than the Redirect method, which redirects immediately. Because the Redirect method requires a round trip to the client and back to the server, you should also see the Server.Transfer and Server.Execute methods (discussed in Chapter 8), which can often accomplish the same purpose without a round trip.

WriteFile The WriteFile method writes the contents of a file to the client. It's overloaded so that you can write partial files by specifying the offset from the start of the file and the number of bytes. For example, the following code accomplishes the same result as the Response.BinaryWrite example shown earlier, but with less code. Comment out the code in the Load event for ch6-l2.aspx.cs and enter this code instead:

Response.Write("<pre>"); Response.WriteFile("c:\\junk.txt"); Response.Write("</pre>");

You will see the same result when you run the example, but it is accomplished with 3 lines of code rather than 10. This method is long overdue for ASP-based sites. For example, you might use this method to hide the filenames of htm or other files that are part of your application.

Was this article helpful?

0 0

Post a comment