• 企业400电话
  • 微网小程序
  • AI电话机器人
  • 电商代运营
  • 全 部 栏 目

    企业400电话 网络优化推广 AI电话机器人 呼叫中心 网站建设 商标✡知产 微网小程序 电商运营 彩铃•短信 增值拓展业务
    Asp.net中Response.Charset与Response.ContentEncoding区别示例分析

    本文以示例形式分析了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程序设计有所帮助。

    您可能感兴趣的文章:
    • asp.net下Response.ContentType类型汇总
    • asp.net 页面转向 Response.Redirect, Server.Transfer, Server.Execute的区别
    • asp.net 使用Response.Filter 过滤非法词汇
    • asp.net中Response.Redirect与Server.Transfer的区别分析
    • ASP.NET笔记之 Request 、Response 与Server的使用
    • ASP.NET之Response.Cookies.Remove 无法删除COOKIE的原因
    • asp.net直接Response输出WML页面示例代码
    • asp.net中WebResponse 跨域访问实例代码
    • Asp.net response对象与request对象使用介绍
    • asp.net内置对象 Response对象使用介绍
    上一篇:asp.net页面SqlCacheDependency缓存实例
    下一篇:拦截asp.net输出流并进行处理的方法
  • 相关文章
  • 

    © 2016-2020 巨人网络通讯

    时间:9:00-21:00 (节假日不休)

    地址:江苏信息产业基地11号楼四层

    《增值电信业务经营许可证》 苏B2-20120278

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