ASP Error “The HTTP headers are already written to the client” Caused by UTF-8 Encoding

ASP error

The HTTP headers are already written to the client. HTTP header modifications must be made before writing page content
ASP scripting compilation error: ‘80020009’

can be caused by your .asp file being in an UTF-8 (with signature) encoding.

To fix the problem, use Visual Studio to convert the encoding of your .asp file to UTF-8 (without signature) or to ANSI. To do this, open your .asp file in VS, then go to File > Advanced Save Options…

In the “Encoding” drop-down box, select “Western European (Windows) – Codepage 1252” (this is equivalent to ANSI) or select “Unicode (UTF-8 without signature) – Codepage 65001”.

ANSI Encoding

UTF-8 Without Signature

If you don’t have Visual Studio, you can use Notepad++ to convert your .asp file to ANSI. To do so, you go to the main menu of Notepad++ and click Encoding > Convert to ANSI.

To put the above info into a bit more of a context, I received the above error when I attempted to set a cookie on my .asp page. Here’s the exact script/html that threw the error:

<% Response.Cookies("id") = "123" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

Note how the <% Response.Cookies("id") = "123" %> script is located at the very top of the page. This is where the script should be since all scripts that append data to the HTTP Headers (cookie info is sent via HTTP Headers) need to be executed prior to all other data UNLESS if the ASP server’s Response.Buffer is set to true (<%response.Buffer=true%>).

Well, my server’s response buffer was set to true and my script was located at the top of the page, so what gives? Why was I still getting the error?

Thanks to John Spaith, I was able to learn what was going on and was able to fix the problem. John posted “UTF-8 in ASP pages on Windows CE” that revealed this odd problem. The main point he made was:

What happens is UTF-8 puts special characters in the first bytes of a file indicating to an editor that it is UTF-8. When our ASP interpreter parses out a page, it sees those UTF-8 characters and (unfortunately) doesn’t know about UTF-8 and assumes its ASP body that needs to be sent out. Once ASP body has been sent, you’re not allowed to send HTTP headers anymore, which is why the Expires=-1 wasn’t working.

I was curious about the “special characters in the first bytes” (I assume the special characters are the “signature” of the UTF-8 encoding) that UTF-8 (with signature) prepends to the data, so I used Binary Viewer. Below is a screenshot of the binaries of the UTF-8 file and the ANSI file. Note: If you have Visual Studio, you can view the binary of your file using the built-in “Binary Editor”. To do so, in the Solution Explorer, right-click the file you want to view, select “Open with…”, then select “Binary Editor”.

UTF-8 – Notice the “extra” data prior to the call to <% Response.Cookies – that extra data is what is causing the error to occur.

ANSI Windows


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s