title: HTTPS Redirect Causing Error "Server cannot append header after HTTP headers have been sent"
body: <p>I need to check that our visitors are using HTTPS.  In BasePage I check if the request is coming via HTTPS.  If it's not, I redirect back with HTTPS. However, when someone comes to the site and this function is used, I get the error:</p>
  
  <blockquote>
    <p>System.Web.HttpException: Server
    cannot append header after HTTP
    headers have been sent. at
    System.Web.HttpResponse.AppendHeader(String
    name, String value) at
    System.Web.HttpResponse.AddHeader(String
    name, String value) at
    Premier.Payment.Website.Generic.BasePage..ctor()</p>
  </blockquote>
  
  <p>Here is the code I started with:</p>
  
  <pre><code>// If page not currently SSL
  if (HttpContext.Current.Request.ServerVariables["HTTPS"].Equals("off"))
  {
      // If SSL is required
      if (GetConfigSetting("SSLRequired").ToUpper().Equals("TRUE"))
      {
          string redi = "https://" +
          HttpContext.Current.Request.ServerVariables["SERVER_NAME"].ToString() +
          HttpContext.Current.Request.ServerVariables["SCRIPT_NAME"].ToString() +
          "?" + HttpContext.Current.Request.ServerVariables["QUERY_STRING"].ToString();
          HttpContext.Current.Response.Redirect(redi.ToString());
      }
  }
  </code></pre>
  
  <p>I also tried adding this above it (a bit I used in another site for a similar problem):</p>
  
  <pre><code>// Wait until page is copletely loaded before sending anything since we re-build
  HttpContext.Current.Response.BufferOutput = true;
  </code></pre>
  
  <p>I am using c# in .NET 3.5 on IIS 6.</p>