.Net/C#: 一个将在线简体中文网页转为繁体中文页简单方法 (尚不完善 IHttpHandler Proxy / Response.Filter)

/*
一个将"任意"在线简体中文网页转为繁体中文页简单方法

以前都写在 Aspx 的 Codebehind 里用 WebRequest 抓别人的网页
ASP 时代用 XMLHTTP Object 抓
1. Microshaoft.HttpHandlers.cs 保存到 IIS 站点或某虚拟目录下的 /bin/Microshaoft.HttpHandlers.cs

2. csc /t:library Microshaoft.HttpHandlers.cs /r:C:/WINNT/Microsoft.NET/Framework/v1.1.4322/Microsoft.VisualBasic.dll
生成 IIS 站点或某虚拟目录下的 /bin/Microshaoft.HttpHandlers.dll

3. IIS 站点或某虚拟目录下 Web.Config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
 <system.web>
  <compilation defaultLanguage="c#" debug="true" />
  <httpHandlers>
   <add verb="*" path="sina.aspx" type="Microshaoft.HttpHandlers.StrConvHttpHandler,Microshaoft.HttpHandlers"/>
  </httpHandlers>
 </system.web>
</configuration>

4.新建文本文件,更名为 sina.aspx ,保存到 IIS 站点或某虚拟目录下 sina.aspx

5.这里只考虑了 get 请求! post 请求请参阅:
补充 宝玉 之《URL欺骗之以假乱真!》之 POST 方法提交数据 之 处理!
http://blog.csdn.net/playyuer/archive/2005/02/25/301895.aspx

6. 了解 IHttpHandler 接口参阅:
ms-help://MS.NETFrameworkSDKv1.1.CHS/cpref/html/frlrfSystemWebIHttpHandlerClassProcessRequestTopic.htm

*/
namespace Microshaoft.HttpHandlers
{
 using System.IO;
 using System.Net;
 using System.Web;
 using System.Text;
 public class StrConvHttpHandler : IHttpHandler
 {
  static void Main()
  {
   StrConvHttpHandler x = new StrConvHttpHandler();
   string s = "http://www.sina.com.cn";
   s = x.HttpGetRequestReponse(s);
   s = Microsoft.VisualBasic.Strings.StrConv(s, Microsoft.VisualBasic.VbStrConv.TraditionalChinese, System.Globalization.CultureInfo.CurrentCulture.LCID);
   //s = TCSCConvert(s);
   System.Console.WriteLine(s);
  }
  public void ProcessRequest(HttpContext context)
  {
   string s = context.Request.RawUrl.ToLower();
   //s = s.Replace("http://localhost:8081","http://210.72.237.99/BOTH/1.2/Blog/portal");
   s = "http://210.72.237.99/BOTH/1.2/Blog/portal/Default.aspx";
   s = "http://www.sina.com.cn";
   s = this.HttpGetRequestReponse(s);
   s = Microsoft.VisualBasic.Strings.StrConv(s, Microsoft.VisualBasic.VbStrConv.TraditionalChinese, System.Globalization.CultureInfo.CurrentCulture.LCID);
   //s = TCSCConvert(s);
   context.Response.Write(s);
  }

  public bool IsReusable
  {
   get
   {
    return true;
   }
  }
  public string HttpGetRequestReponse(string URL)
  {
   HttpWebRequest hwrq = (HttpWebRequest) WebRequest.Create(URL);
   HttpWebResponse hwrp = (HttpWebResponse) hwrq.GetResponse();
   Stream S = hwrp.GetResponseStream();
   StreamReader sr = new StreamReader(S,this.GetEncoding(hwrp));
   string ss = null;
   string s;
   while( (s = sr.ReadLine())!=null )
   {
    ss += s + "/r/n";
   }
   S.Close();
   S = null;
   return ss;
  }
  public Encoding GetEncoding(HttpWebResponse response)
  {
   string name = response.ContentEncoding;
   Encoding code = Encoding.Default;
   if (name == "")
   {
    string contentType = response.ContentType;
    if (contentType.ToLower().IndexOf("charset") != -1)
    {
     name = contentType.Substring(contentType.ToLower().IndexOf("charset=") + "charset=".Length);
    }
   }
   if (name != "")
   {
    try
    {
     code = Encoding.GetEncoding(name);
    }
    catch{}
   }
   return code;
  }
  //经测试,用 COM Word Object 转的效果全好,但是慢!
  //private static Word.DocumentClass _d;
  private static string TCSCConvert(string s)
  {
   if (s.Length > 0)
   {
    //    if (_d == null)
    //    {
    //     _d = new Word.DocumentClass();
    //    }
    //    _d.Content.Text = s;
    //    _d.Content.TCSCConverter(
    //     Word.WdTCSCConverterDirection.wdTCSCConverterDirectionSCTC,
    //     true,
    //     true
    //     );
    //    s = _d.Content.Text;
   }
   return s;
  }
 }
}

如果是自己写的 Asp.Net 的页面还有更好的方法:
自己实现一个类继承于 Stream,作为 HttpResponse 的 Filter 属性:
namespace Microshaoft //(/bin/XXX.dll,也可直接放在 Page 的 codebehind)
{
 using System;
 using System.Text;
 using System.Text.RegularExpressions;
 using System.IO;
 using System.Web;

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

  public StrConvFilter(Stream sink)
  {
   _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 _position;
   }
   set
   {
    _position = value;
   }
  }

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

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

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

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

  public override int Read(byte[] buffer, int offset, int count)
  {
   return _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 = Microsoft.VisualBasic.Strings.StrConv(s, Microsoft.VisualBasic.VbStrConv.TraditionalChinese, System.Globalization.CultureInfo.CurrentCulture.LCID);
    _sink.Write(e.GetBytes(s), 0, e.GetByteCount(s));
   }
   else
   {
    _sink.Write(buffer, offset, count);
   }
  }
 }
}

然后:
private void Page_Load(object sender, System.EventArgs e)
{
 // 在此处放置用户代码以初始化页面
 this.Response.Filter = new Microshaoft.StrConvFilter(this.Response.Filter);
}

 

posted @ 2005-03-08 15:12  于斯人也  阅读(239)  评论(0编辑  收藏  举报