Sample Analysis of differences between Response. Charset and Response. ContentEncoding in Asp.net,

Source: Internet
Author: User

Sample Analysis of differences between Response. Charset and Response. ContentEncoding in Asp.net,

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

1. Response. Charset
Example in ASP. NET:

<%@ Page CodePage=936 %>

CodePage tells IIS what encoding to read QueryString and what encoding to convert the content in the database ......

2. Response. ContentEncoding

Gets or sets the HTTP Character Set of the output stream.

Response. Charset

Gets or sets the HTTP Character Set of the output stream. Microsoft's interpretation of ContentEncoding and Charset is a word. In fact, it can be understood as follows: ContentEncoding identifies the encoding of the content, and Charset tells the client how to display it.

We can give an example to understand:

Example 1.

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

Then open the web page in a browser and you will find garbled characters. However, you can use NotePad to view the source file and find that it is not garbled. This shows that ContentEncoding is used to stream bytes to text, while Charset is displayed in a browser.

Example 2.

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

Using Fidller, we found that the HTTP header is: text/html; charset = gb2312. It indicates that when Charset is not specified, the Charset of ContentEncoding is used as the charset.

Example 3.

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

The HTTP header is text/html; charset = 123-8. The page is displayed normally, indicating that if charset is incorrect, the Charset of ContentEncoding is still used as the charset.

Example 4.

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

The HTTP header is text/html ;. There is no charset in the HTTP header, and the webpage is displayed normally, indicating that there is no charset in the HTTP header, and the Charset of ContentEncoding is still used as the charset.

Supplement:

I. Response. ContentType

Obtain or set the http mime type in the output stream, such as text/xml, text/html, and application/ms-word. The browser enables different engines based on different content. For example, in IE6 or later versions, XML is automatically displayed in a tree.

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

This is a tag in HTML and cannot be used in XML, JS, and other files. It is used to tell the browser the MIME and Character Set of the webpage. The current content does not refer to the timing. The browser determines this.

2. Use a manifold as an example of a word file

Protected void btnResponseWord_Click (object sender, EventArgs e) {Response. clear (); // Clear irrelevant information Response. buffer = true; // send Response after completing the Response. charset = "GB2312"; // set the character set of the output stream-Chinese Response. appendHeader ("Content-Disposition", "attachment?filename=report.doc"); // append header information Response. contentEncoding = System. text. encoding. getEncoding ("GB2312"); // sets the character set Response of the output stream. contentType = "application/ms-word"; // The MIME type Response of the output stream. write (TextBox1.Text); Response. end (); // stop output}

3. Use Response. AppendHeader

@ File download, specifying the default name

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

@ Refresh page

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

This is equivalent to the client <META> element:

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

@ Page redirection

Response.Status = "302 Object Moved"Response.Addheader "Location", "newpath/newpage.asp"

This is equivalent to using the Response. Redirect method:

Response.Redirect "newpath/newpage.asp"

@ Force the browser to display a user name/Password dialog box

Response.Status= "401 Unauthorized"Response.Addheader "WWW-Authenticate", "BASIC"

Force the browser to display a user name/Password dialog box, and then send them back to the server using BASIC verification (the verification method will be seen later in this book ).
@ How To Make webpages unbuffered

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

We hope that the differences between Response. Charset and Response. ContentEncoding in Asp.net described in this article and their usage will be helpful for the Asp.net program design.


C # What is the difference between ResponseCharset and ResponseContentEncoding?

Msdn.microsoft.com/..harset (VS.80). aspx

Msdn.microsoft.com/..coding (VS.80). aspx
Watch it for yourself

How does the output statement ResponseWrite in aspnet differ from that in PageResponseWrite?

It calls something.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.