Response.Charset and Response.ContentEncoding: Asp.net: Response.Charset: Response

  • 2021-01-18 06:23:38
  • OfStack

This paper analyzes the differences between Response.Charset and Response.ContentEncoding in Asp.net in the form of examples, and shares it with you for your reference. The details are as follows:

1.Response.Charset
ES11en. ES12en Examples of ES11en:


<%@ Page CodePage=936 %>

CodePage tells IIS what code to read QueryString, what code to convert the contents of the database...

2.Response.ContentEncoding

Gets or sets the HTTP character set for the output stream.

Response.Charset

Gets or sets the HTTP character set for the output stream. ES34en identifies what the content is encoded in, while ES35en tells the client how to display it.

We can do an example to understand:

Example 1.


Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.Charset = "utf-8"; 
Response.Write(" The home of the script ");

Then open the web page with the browser, you can find that it is garbled, but with the notepad to view the source file, and found that it is not garbled. This explains that ES43en streams bytes to text, while ES44en displays bytes in the browser.

Example 2.


Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");

Through Fidller, found HTTP header is: text/html; charset = gb2312. When Charset is not specified, the Charset of ContentEncoding is used as charset.

Example 3.


Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.Charset = "123-8";

HTTP header: text/html; charset = 123-8. Charset of ContentEncoding is still used as charset if charset is wrong.

Example 4.


Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.Charset = "";

HTTP header is: text/html; . There is no charset in the header of HTTP. The page displays normally. This means that there is no charset in the header of HTTP.

Supplement:

1.Response.ContentType

Gets or sets the MIME type of HTTP in the output stream, such as text/xml, text/html, application/ms-word. Browsers enable different engines for different content, such as IE6 and above, which automatically display XML as a tree.


<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />

This is the HTML tag. It cannot be used in XML, JS, etc. It tells the browser the MIME character set of the web page. This is where the browser determines if the relevant content is not specified.

2. Use the stream to form an example word file


protected void btnResponseWord_Click(object sender, EventArgs e)
{
    Response.Clear(); // Empty irrelevant information 
    Response.Buffer= true; // Send the entire response after it is complete 
    Response.Charset = "GB2312";// Sets the character set of the output stream - Chinese 
 Response.AppendHeader("Content-Disposition","attachment;filename=Report.doc");// Append header 
    Response.ContentEncoding = System.Text.Encoding.GetEncoding("GB2312");// Sets the character set of the output stream 
    Response.ContentType = "application/ms-word ";// The output stream MIME type 
    Response.Write(TextBox1.Text);
    Response.End();// Stop output 
}

3. Response. AppendHeader use

@ file download, specifying the default name


Response.AddHeader("content-type","application/x-msdownload");
Response.AddHeader("Content-Disposition","attachment;filename= The file name to download .rar");

@ Refresh the page


Response.AddHeader "REFRESH", "60;URL=newpath/newpage.asp"

This is equivalent to the client side < META > Chemical element:


<META HTTP-EQUIV="REFRESH", "60;URL=newpath/newpage.asp"

@page redirection


Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.Charset = "utf-8"; 
Response.Write(" The home of the script ");
0

This is equivalent to using the Response.Redirect method:


Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.Charset = "utf-8"; 
Response.Write(" The home of the script ");
1

@Forces the browser to display a username/password dialog


Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");
Response.Charset = "utf-8"; 
Response.Write(" The home of the script ");
2

Force the browser to display a username/password dialog box, and then send them back to the server using BASIC authentication (you'll see validation later in this book).
How to make web pages not buffered


Response.Expires = 0
Response.ExpiresAbsolute = Now() - 1
Response.Addheader "pragma","no-cache"
Response.Addheader "cache-control","private"
Response.CacheControl = "no-cache

Response.Charset and Response.ContentEncoding. Asp.net. Asp.net.


Related articles: