Use ihttpmodule to solve Chinese address garbled characters (1)

Source: Internet
Author: User
Test environment:

Server -- [Local Machine] wiindows2003 Enterprise Edition 2003 sp2 Chinese version, IIS 6.0, ie 7.0, ASP. NET 2.0
Client -- [Local Machine] Same as above, Firefox 2.0.0.12

Author: birdshover

This article is a preliminary discussionArticle. We need to make several assumptions:
1. The website is deployed using ASP. NET;
2. The website code is UTF-8;
3. Modifying page links is costly.

Currently, the main problem is that there are many links in the page, and the parameters directly contain Chinese characters, which is more friendly than the URL after transcoding. However, when user a sends a nice address to friend B, the problem occurs and the parameter will become garbled!

In fact, in the UTF-8 environment, the link on the page on the website can be Chinese, for example:
<A href = "http: // localhost/a. aspx? Key = Chinese "> http: // localhost/a. aspx? Key = Chinese </a>

However, copy "http: // localhost/a. aspx? Key = is Chinese. If you directly enter the address bar, garbled characters are generated.

This article aims to solve the problem when entering Chinese characters in the address bar. This method is simple, but it may conflict with existing links. The method to avoid conflicts is complicated and will be introduced in subsequent articles.

Using. Net httpmodule rewriting can solve the above problems.

Construct an httpmodule as follows:Public ClassHookmodule: ihttpmodule
{

# RegionIhttpmodule Member

Public VoidDispose ()
{

}

Public VoidInit (httpapplication context)
{
Context. beginrequest+ = NewEventhandler (context_beginrequest );
}

Void Context_beginrequest ( Object Sender, eventargs E)
{
Httpapplication = (Httpapplication) sender;
Httpcontext Context = Application. context;
String Rawurl = Context. Request. rawurl;

Context. rewritepath (rawurl );

}

# Endregion
}

After the configuration is complete, enter "http: // localhost/a. aspx?" in the address bar? Key = is Chinese. No gibberish (ie, theworld 2.0) is generated ).

However, when you enter the above address in Firefox, it turns into garbled characters.
That's because Firefox forcibly encodes the address. SetCodeTransformed:

Public   Class Hookmodule: ihttpmodule
{

# RegionIhttpmodule Member

Public VoidDispose ()
{

}

Public VoidInit (httpapplication context)
{
Context. beginrequest+ = NewEventhandler (context_beginrequest );
}

Void Context_beginrequest ( Object Sender, eventargs E)
{
Httpapplication = (Httpapplication) sender;
Httpcontext Context = Application. context;
String Rawurl = Context. Request. rawurl;
Byte [] Bytes = System. Web. httputility. urldecodetobytes (rawurl, encoding. Default );
String S = Encoding. Default. getstring (bytes );
Context. rewritepath (s );

}

# Endregion
}

OK. The problem is solved and the display is normal in all three browsers.

Do not end here because the problem may have occurred.

Click <a href = "http: // localhost/a. aspx? Key = Chinese "> http: // localhost/a. aspx? Key = Chinese </a>
This address turns into garbled characters.

The solution in this article is to encode the address
System. Web. httputility. urlencode ("Chinese", encoding. getencoding ("gb2312 "))
In this way, there is no garbled problem.

Note that the above Code uses encoding. Default because my system is in the Chinese version, the default encoding is gb2312.

The full text is complete.
From: http://www.cnblogs.com/birdshover/

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.