Two simple methods to automatically convert Simplified Chinese web pages to traditional Chinese web pages (the principle is very simple, and ihttphandler proxy/response. filter is not perfect)

Source: Internet
Author: User

I:

/*

A simple method to convert an "arbitrary" online Simplified Chinese webpage to a traditional Chinese webpage

I used to write in codebehind of aspx and use webrequest to capture others' webpages.

Using XMLHTTP object in ASP

1. Save microshaoft. httphandlers. CS to the IIS Site or \ bin \ microshaoft. httphandlers. CS in a virtual directory.

2. CSC/T: Library microshaoft. httphandlers. CS/R: C: \ winnt \ Microsoft. NET \ framework \ v1.1.4322 \ Microsoft. VisualBasic. dll

Generate \ bin \ microshaoft. httphandlers. dll under the IIS site or a virtual directory

3. IIS Site or web. config in a virtual directory:

<? 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. Create a text file, rename it sina. aspx, and save it to the IIS site or a virtual directory.

5. Only get requests are considered here! For post requests, see:

Supplement the URL Spoofing with treasure! Processing of data submitted by post method!

Http://blog.csdn.net/playyuer/archive/2005/02/25/301895.aspx

6. For more information about ihttphandler interfaces, 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 = 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;

}

// After testing, the effect of converting with COM word object is good, but it is 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. wdtcscconverterdirection. wdtcscconverterdiresctc,

// True,

// True

//);

// S = _ d. content. text;

}

Return S;

}

}

}

// ================================================ ====

II:

There are better methods for self-written ASP. NET pages:

Implement a class inherited from stream as the filter attribute of httpresponse:

Namespace microshaoft // (\ bin \ XXX. dll, which can also be directly stored in codebehind of page)

{

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)

{< br>
If (httpcontext. current. response. contenttype = "text/html")

{< br>
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);

}< br>
else

{< br>
_ sink. write (buffer, offset, count );

}

then:
private void page_load (Object sender, system. eventargs E)
{
// place the user Code here to initialize the page
> This. response. filter = new microshaoft. strconvfilter (this. response. filter);

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.