.NETC #: A simple method to convert online simplified Chinese webpage to Traditional Chinese

xiaoxiao2021-03-05  20

/ * A simple method for turning "any" online simplified Chinese web page to Traditional Chinese

I used to write in the ASPX's codebehind with WebRequest to catch other people's web page ASP eras to use XMLHTTP Object to capture 1. Microshaoft.htttphandlers.cs Save to IIS Site or a virtual directory /bin/microshaoft.httphandlers.cs

2. CSC / T: library microshaoft.httphandlers.cs /r:C:/Winnt/Microsoft.net/framework/v1.1.4322/Microsoft.visualbasic.dll Generates /bin/microshaoft.httphandlers in a virtual directory .dll

3. IIS site or virtual directory Web.config:

4. New text files, renamed Sina.aspx, save to IIS site or sina.aspx in a virtual directory

5. Here only takes the GET request! POST request, please refer to the "URL deception of the URL, the POST method submitted to the data! Http://blog.9cbs.net/playyuer/archive/2005/02/02/02/02 25 / 301895.aspx

6. Understand the IHTTPHANDLER interface See: 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); 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);}} f (name! =") {Try {code = encoding.getencoding (name);} catCh}}} Return code;} // test, using COM Word Object turn effect Ok, but slow ! // 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.wdtcscconvertirection.wdtcscconvert000 )ctc, // true, // true //); // s = _d.content.text } Return s;}}}}}}}

If it is a page written asP.NET, there is a better way: you can implement a class inherited in stream, which is the Filter property of httpresponse: namespace microshaoft // (/bin/xxx.dll, you can also put it directly in 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 {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, ELETBYTECOUNT (s));} else {_sink.write (buffer, offset, count);}}}}

转载请注明原文地址:https://www.9cbs.com/read-33158.html

New Post(0)