A sample analysis of the difference between Response.Charset and response.contentencoding in asp.net _ Practical skills

Source: Internet
Author: User

This paper analyses the difference between response.charset and response.contentencoding in ASP.net, and shares it for everyone's reference. Specifically as follows:

1.response.charset
examples in asp.net:

<%@ Page codepage=936%>

CodePage tells IIS what encoding to read QueryString, what encoding 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. Microsoft's interpretation of ContentEncoding, Charset is a word, in fact, it can be understood that: contentencoding is to identify what this content is encoded, and Charset is to tell the client how to display.

We can do an example to understand:

Example 1.

response.contentencoding = System.Text.Encoding.GetEncoding ("gb2312");
Response.Charset = "Utf-8"; 
Response.Write ("cloud-dwelling community");

Then use the browser to open the Web page, you can find garbled, but with Notepad to view the source file, and found that it is not garbled. This explains: ContentEncoding is the byte stream to the text, and Charset is the tube displayed in the browser.

Example 2.

response.contentencoding = System.Text.Encoding.GetEncoding ("gb2312");

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

Example 3.

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

HTTP headers are: text/html; Charset=123-8. The Web page appears normal, indicating that if CharSet error, still take contentencoding CharSet as CharSet.

Example 4.

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

HTTP headers are: text/html; There is no charset in the HTTP header, the Web page appears normal, indicating that there is no charset in the HTTP header, and still contentencoding CharSet as the CharSet.

Add:

I. 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 based on different content, such as IE6 and the above version automatically makes the XML tree-like display.

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

This is the HTML tag, can not be used in XML, JS and other files, it is to tell the browser Web page MIME, character set. The browser uses this to determine when the previous relevant content is not specified.

Two. Use flow to form a Word file example

protected void Btnresponseword_click (object sender, EventArgs e)
{
    response.clear ();//Empty extraneous information
    Response.buffer= true; Complete the response and then send
    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 )//Set the output stream's character set
    Response.ContentType = "Application/ms-word";//output stream MIME type
    Response.Write (TextBox1.Text);
    Response.End ()//Stop Output
}

Three. Response.appendheader use

@ file Download, specify default name

Response.AddHeader ("Content-type", "application/x-msdownload");
Response.AddHeader ("Content-disposition", "Attachment;filename= file name to be downloaded. rar");

@ Refresh Page

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

This equates to the client side <META> elements:

<meta http-equiv= "REFRESH", "60; Url=newpath/newpage.asp "

@ Page Turn

Response.Status = "302 Object moved"
Response.AddHeader "Location", "newpath/newpage.asp"

This equates to using the Response.Redirect method:

Response.Redirect "Newpath/newpage.asp"

@ Force browser to display a username/password dialog box

Response.status= "401 Unauthorized"
Response.AddHeader "Www-authenticate", "BASIC"

Force the browser to display a username/password dialog box, and then use Basic authentication to send them back to the server (the validation method will be seen later in this book).
@ How to make pages not buffered

Response.Expires = 0
Response.ExpiresAbsolute = Now ()-1
response.addheader "Pragma", "No-cache"
Response.AddHeader "Cache-control", "private"
Response.CacheControl = "No-cache

It is hoped that the differences between Response.Charset and response.contentencoding in the asp.net mentioned in this paper and their related usages will help asp.net program design.

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.