当前位置: 移动技术网 > IT编程>开发语言>.net > Asp.net中Response.Charset与Response.ContentEncoding区别示例分析

Asp.net中Response.Charset与Response.ContentEncoding区别示例分析

2017年12月12日  | 移动技术网IT编程  | 我要评论

暮春堂最新地址,笔上难写心上情,华硕a41l

本文以示例形式分析了asp.net中response.charset与response.contentencoding的区别,分享给大家供大家参考。具体如下:

1.response.charset 
asp.net 中示例:

<%@ page codepage=936 %>

codepage 告诉 iis 按什么编码来读取 querystring,按什么编码转换数据库中的内容……

2.response.contentencoding

获取或设置输出流的 http 字符集。

response.charset

获取或设置输出流的 http 字符集。微软对 contentencoding、charset 的解释是一字不差,其实可以这样理解:contentencoding 是标识这个内容是什么编码的,而 charset 是告诉客户端怎么显示的。

我们可以做一个示例来理解:

示例1.

response.contentencoding = system.text.encoding.getencoding("gb2312");
response.charset = "utf-8"; 
response.write("移动技术网");

然后用浏览器打开网页,可以发现是乱码,可是用记事本查看源文件,又发现不是乱码。这就说明了:contentencoding 是管字节流到文本的,而 charset 是管在浏览器中显示的。

示例2.

response.contentencoding = system.text.encoding.getencoding("gb2312");

通过 fidller,发现 http 头中是:text/html; charset=gb2312。说明没有指定 charset 时,就用 contentencoding 的 charset 作为 charset。

示例3.

response.contentencoding = system.text.encoding.getencoding("gb2312");
response.charset = "123-8";

http 头中是:text/html; charset=123-8。网页显示正常,说明如果 charset 错误,仍然以 contentencoding 的 charset 作为 charset。

示例4.

response.contentencoding = system.text.encoding.getencoding("gb2312");
response.charset = "";

http 头中是:text/html;。http 头中没有 charset,网页显示正常,说明 http 头中没有 charset,仍然以 contentencoding 的 charset 作为 charset。

补充:

一.response.contenttype

获取或设置输出流中 http 的 mime 类型,比如:text/xml、text/html、application/ms-word。浏览器根据不同的内容启用不同的引擎,比如 ie6 及以上版本中就会自动将 xml 做成树状显示。

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

这是 html 中的标签,不能用在 xml、js 等文件中,它是告诉浏览器网页的 mime、字符集。当前面的相关内容没有指定时,浏览器通过此来判断。

二.使用流形成一个word文件例子

protected void btnresponseword_click(object sender, eventargs e)
{
    response.clear(); //清空无关信息
    response.buffer= true; //完成整个响应后再发送
    response.charset = "gb2312";//设置输出流的字符集-中文
 response.appendheader("content-disposition","attachment;filename=report.doc");//追加头信息
    response.contentencoding = system.text.encoding.getencoding("gb2312");//设置输出流的字符集
    response.contenttype = "application/ms-word ";//输出流的mime类型
    response.write(textbox1.text);
    response.end();//停止输出
}

三.response.appendheader使用

@文件下载,指定默认名

response.addheader("content-type","application/x-msdownload");
response.addheader("content-disposition","attachment;filename=要下载的文件名.rar");

@刷新页面

response.addheader "refresh", "60;url=newpath/newpage.asp"

这等同于客户机端<meta>元素:

<meta http-equiv="refresh", "60;url=newpath/newpage.asp"

@页面转向

response.status = "302 object moved"
response.addheader "location", "newpath/newpage.asp"

这等同于使用response.redirect方法:

response.redirect "newpath/newpage.asp"

@强制浏览器显示一个用户名/口令对话框

response.status= "401 unauthorized"
response.addheader "www-authenticate", "basic"

强制浏览器显示一个用户名/口令对话框,然后使用basic验证把它们发送回服务器(将在本书后续部分看到验证方法)。
@如何让网页不缓冲

response.expires = 0
response.expiresabsolute = now() - 1
response.addheader "pragma","no-cache"
response.addheader "cache-control","private"
response.cachecontrol = "no-cache

希望本文所述的asp.net中response.charset与response.contentencoding的区别及相关用法对大家asp.net程序设计有所帮助。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网