diff --git a/lib/SimpleSAML/Error/BadRequest.php b/lib/SimpleSAML/Error/BadRequest.php
index f7dabdb54ba5a2ac875f4bb7b2cbfa8df9c9c932..8727c7cb1e63c545a6069fcfd3c5ac353e34c79a 100644
--- a/lib/SimpleSAML/Error/BadRequest.php
+++ b/lib/SimpleSAML/Error/BadRequest.php
@@ -29,6 +29,7 @@ class SimpleSAML_Error_BadRequest extends SimpleSAML_Error_Error {
 
 		$this->reason = $reason;
 		parent::__construct(array('BADREQUEST', '%REASON%' => $this->reason));
+		$this->httpCode = 400;
 	}
 
 
@@ -41,16 +42,4 @@ class SimpleSAML_Error_BadRequest extends SimpleSAML_Error_Error {
 		return $this->reason;
 	}
 
-
-	/**
-	 * Set the HTTP return code for this error.
-	 *
-	 * This should be overridden by subclasses who want a different return code than 500 Internal Server Error.
-	 */
-	protected function setHTTPCode() {
-		header('HTTP/1.0 400 Bad Request');
-	}
-
 }
-
-?>
\ No newline at end of file
diff --git a/lib/SimpleSAML/Error/Error.php b/lib/SimpleSAML/Error/Error.php
index 3772f8869fdd6f03298893a4357dbbecb1867bf2..86de994b0e93e5185b9a802ea98e760295c16ace 100644
--- a/lib/SimpleSAML/Error/Error.php
+++ b/lib/SimpleSAML/Error/Error.php
@@ -18,6 +18,14 @@ class SimpleSAML_Error_Error extends SimpleSAML_Error_Exception {
 	private $errorCode;
 
 
+	/**
+	 * The http code.
+	 *
+	 * @var integer
+	 */
+	protected $httpCode = 500;
+
+
 	/**
 	 * The error title tag in dictionary.
 	 *
@@ -68,7 +76,7 @@ class SimpleSAML_Error_Error extends SimpleSAML_Error_Exception {
 	 * @param mixed $errorCode  One of the error codes defined in the errors dictionary.
 	 * @param Exception $cause  The exception which caused this fatal error (if any).
 	 */
-	public function __construct($errorCode, Exception $cause = NULL) {
+	public function __construct($errorCode, Exception $cause = NULL, $httpCode = NULL) {
 		assert('is_string($errorCode) || is_array($errorCode)');
 
 		if (is_array($errorCode)) {
@@ -80,6 +88,10 @@ class SimpleSAML_Error_Error extends SimpleSAML_Error_Exception {
 			$this->errorCode = $errorCode;
 		}
 
+		if (isset($httpCode)) {
+			$this->httpCode = $httpCode;
+		}
+
 		$moduleCode = explode(':', $this->errorCode, 2);
 		if (count($moduleCode) === 2) {
 			$this->module = $moduleCode[0];
@@ -153,7 +165,30 @@ class SimpleSAML_Error_Error extends SimpleSAML_Error_Exception {
 	 * This should be overridden by subclasses who want a different return code than 500 Internal Server Error.
 	 */
 	protected function setHTTPCode() {
-		header('HTTP/1.0 500 Internal Server Error');
+		// Some mostly used HTTP codes.
+		$httpCodesMap = array(
+			400 => 'HTTP/1.0 400 Bad Request',
+			403 => 'HTTP/1.0 403 Forbidden',
+			404 => 'HTTP/1.0 404 Not Found',
+			405 => 'HTTP/1.0 405 Method Not Allowed',
+			500 => 'HTTP/1.0 500 Internal Server Error',
+			501 => 'HTTP/1.0 501 Method Not Implemented',
+			503 => 'HTTP/1.0 503 Service Temporarily Unavailable',
+		);
+
+		$httpCode = $this->httpCode;
+
+		if (function_exists('http_response_code')) {
+			http_response_code($httpCode);
+			return;
+		}
+
+		if (!array_key_exists($this->httpCode, $httpCodesMap)) {
+			$httpCode = 500;
+			SimpleSAML_Logger::warning('HTTP response code not defined: ' . var_export($this->httpCode, TRUE));
+		}
+
+		header($httpCodesMap[$httpCode]);
 	}
 
 
diff --git a/lib/SimpleSAML/Error/NotFound.php b/lib/SimpleSAML/Error/NotFound.php
index fd2406760c31d1e72f4e34b9a58dd838e85d467e..d530c0da3135c0be71ab1eada0226b93aea6f3a9 100644
--- a/lib/SimpleSAML/Error/NotFound.php
+++ b/lib/SimpleSAML/Error/NotFound.php
@@ -37,6 +37,7 @@ class SimpleSAML_Error_NotFound extends SimpleSAML_Error_Error {
 		}
 
 		$this->reason = $reason;
+		$this->httpCode = 404;
 	}
 
 
@@ -49,16 +50,4 @@ class SimpleSAML_Error_NotFound extends SimpleSAML_Error_Error {
 		return $this->reason;
 	}
 
-
-	/**
-	 * Set the HTTP return code for this error.
-	 *
-	 * This should be overridden by subclasses who want a different return code than 500 Internal Server Error.
-	 */
-	protected function setHTTPCode() {
-		header('HTTP/1.0 404 Not Found');
-	}
-
 }
-
-?>
\ No newline at end of file