HttpServer.HttpResponse.AddHeader C# (CSharp) Method

AddHeader() public method

Add another header to the document.
Adding any header will override the default ones and those specified by properties.
If headers already been sent. If value conditions have not been met.
public AddHeader ( string name, string value ) : void
name string Name of the header, case sensitive, use lower cases.
value string Header values can span over multiple lines as long as each line starts with a white space. New line chars should be \r\n
return void
        public void AddHeader(string name, string value)
        {
            if (HeadersSent)
                throw new InvalidOperationException("Headers have already been sent.");

            for (int i = 1; i < value.Length; ++i )
            {
                if (value[i] == '\r' && !char.IsWhiteSpace(value[i-1]))
                    throw new ArgumentException("New line in value do not start with a white space.");
                if (value[i] == '\n' && value[i-1] != '\r')
                    throw new ArgumentException("Invalid new line sequence, should be \\r\\n (crlf).");
            }

            _headers[name] = value;
        }

Usage Example

 /// <summary>
 /// Will request authentication.
 /// </summary>
 /// <remarks>
 /// Sends respond to client, nothing else can be done with the response after this.
 /// </remarks>
 /// <param name="mod"></param>
 /// <param name="request"></param>
 /// <param name="response"></param>
 protected virtual void RequestAuthentication(AuthenticationModule mod, HttpRequest request, HttpResponse response)
 {
     string theResponse = mod.CreateResponse(GetRealm(request));
     response.AddHeader("www-authenticate", theResponse);
     response.Reason = "Authentication required.";
     response.Status = HttpStatusCode.Unauthorized;
 }
All Usage Examples Of HttpServer.HttpResponse::AddHeader