当前位置:编程学习 > C#/ASP.NET >>

asp.net 繁体环境下简体转换繁体

我的程序在简体环境下可以转换成功,但是在繁体环境下转换后都是乱码,这是什么原因?怎么解决?
方法如下:
1、配置文件:<httpModules>
                <add name="StrConvHttpModule" type="HttpModules.StrConvHttpModule, ClassLibrary1" />
             </httpModules>
2、ClassLibrary1.dll是两个文件编译出来的:
第一个:
using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Collections;
using zhuanhuan.IO;

namespace HttpModules
{
     public class StrConvHttpModule : IHttpModule  
     {  
         public string ModuleName  
         {  
             get  
             {  
                 return "StrConvHttpModule";  
             }  
         }  
   
         public void Init(HttpApplication application)  
         {  
             application.BeginRequest += (new EventHandler(this.Application_BeginRequest));  
         }  
           
         private void Application_BeginRequest(object sender, EventArgs e)  
         {  
             HttpApplication application = (HttpApplication) sender;  
             HttpContext context = application.Context;  
             context.Response.Filter = new StrConvFilter(context.Response.Filter);  
         }  
   
         public void Dispose()  
         {  
         }  
     }  
 }

第二个:
using System;
using System.Collections.Generic;
using System.Text;

namespace zhuanhuan.IO
{
    using System;
    using System.IO;
    using System.Web;
    using System.Text;
    using System.Globalization;

    using Microsoft.VisualBasic;

    public class StrConvFilter : Stream
    {
        private Stream _sink;
        private long _position;

        public StrConvFilter(Stream sink)
        {
            this._sink = sink;
        }

        public override bool CanRead
        {
            get
            {
                return true;
            }
        }

        public override bool CanSeek
        {
            get
            {
                return true;
            }
        }

        public override bool CanWrite
        {
            get
            {
                return true;
            }
        }

        public override long Length
        {
            get
            {
                return 0;
            }
        }

        public override long Position
        {
            get
            {
                return this._position;
            }
            set
            {
                this._position = value;
            }
        }

        public override long Seek(long offset, SeekOrigin direction)
        {
            return this._sink.Seek(offset, direction);
        }

        public override void SetLength(long length)
        {
            this._sink.SetLength(length);
        }

        public override void Close()
        {
            this._sink.Close();
        }

        public override void Flush()
        {
            this._sink.Flush();
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            return this._sink.Read(buffer, offset, count);
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            if (HttpContext.Current.Response.ContentType == "text/html")
            {
                Encoding e = Encoding.GetEncoding(HttpContext.Current.Response.Charset);
                string s = e.GetString(buffer, offset, count);
                s = Strings.StrConv(s, VbStrConv.TraditionalChinese, CultureInfo.CurrentCulture.LCID);
                this._sink.Write(e.GetBytes(s), 0, e.GetByteCount(s));
            }
            else
            {
                this._sink.Write(buffer, offset, count);
            }
        }
    }
}
--------------------编程问答-------------------- 怎么没有人进来啊?
--------------------编程问答-------------------- 没遇到过,帮顶下 --------------------编程问答-------------------- 没有人知道吗?
--------------------编程问答-------------------- 感觉你的逻辑和代码有点不一致

CHS平台应该是转化成CHT

CHT平台应该是转化成CHS

--------------------编程问答-------------------- 最后的Write方法改下
public override void Write(byte[] buffer, int offset, int count)
  {
  if (HttpContext.Current.Response.ContentType == "text/html")
  {
  Encoding e = Encoding.GetEncoding(HttpContext.Current.Response.Charset);
  Encoding e = HttpContext.Current.Response.ContentEncoding;

  string s = e.GetString(buffer, offset, count);
  s = Strings.StrConv(s, VbStrConv.TraditionalChinese, CultureInfo.CurrentCulture.LCID);
  s = Strings.StrConv(s, VbStrConv.TraditionalChinese, 2052);
  this._sink.Write(e.GetBytes(s), 0, e.GetByteCount(s));
  }
  else
  {
  this._sink.Write(buffer, offset, count);
  }
  }
  } --------------------编程问答-------------------- http://www.microsoft.com/china/msdn/library/webservices/asp.net/BasicInstincts0608.mspx?mfr=true  --------------------编程问答-------------------- 有简体和繁体的转换包 你可以去找找,顺便顶一下楼上的。。牛人啊! --------------------编程问答--------------------
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,