System.Web.HttpResponse.WriteHeaders C# (CSharp) Method

WriteHeaders() private method

private WriteHeaders ( bool final_flush ) : void
final_flush bool
return void
		internal void WriteHeaders (bool final_flush)
		{
			if (headers_sent)
				return;

			//
			// Flush
			//
			if (context != null) {
				HttpApplication app_instance = context.ApplicationInstance;
				if (app_instance != null)
					app_instance.TriggerPreSendRequestHeaders ();
			}

			headers_sent = true;

			if (cached_response != null)
				cached_response.SetHeaders (headers);

			// If this page is cached use the cached headers
			// instead of the standard headers	
			NameValueCollection write_headers;
			if (cached_headers != null)
				write_headers = cached_headers;
			else {
				write_headers = Headers;
				AddHeadersNoCache (write_headers, final_flush);
			}
			
			if (WorkerRequest != null)
				WorkerRequest.SendStatus (status_code, StatusDescription);

			if (WorkerRequest != null) {
				string header_name;
				string[] values;
				int header_index;
				
				for (int i = 0; i < write_headers.Count; i++) {
					header_name = write_headers.GetKey (i);
					header_index = HttpWorkerRequest.GetKnownResponseHeaderIndex (header_name);
					values = write_headers.GetValues (i);
					if (values == null)
						continue;
					
					foreach (string val in values) {
						if (header_index > -1)
							WorkerRequest.SendKnownResponseHeader (header_index, val);
						else
							WorkerRequest.SendUnknownResponseHeader (header_name, val);
					}
				}
			}
		}

Usage Example

        public void Write(char [] buffer, int offset, int count)
        {
            bool buffering = response.BufferOutput;

            if (buffering)
            {
                // It does not matter whether we're in ApplyFilter or not
                AppendBuffer(buffer, offset, count);
            }
            else if (filter == null || filtering)
            {
                response.WriteHeaders(false);
                HttpWorkerRequest wr = response.WorkerRequest;
                // Direct write because not buffering
                wr.SendResponseFromMemory(buffer, offset, count, response.ContentEncoding);
                wr.FlushResponse(false);
            }
            else
            {
                // Write to the filter, which will call us back, and then Flush
                filtering = true;
                try {
                    byte [] bytesToWrite = response.ContentEncoding.GetBytes(buffer, offset, count);
                    filter.Write(bytesToWrite, 0, bytesToWrite.Length);
                }
                finally {
                    filtering = false;
                }
                Flush(response.WorkerRequest, false);
            }
        }