Experience in connecting COOKIES between ASP. NET and ASP

Source: Internet
Author: User

After Microsoft launched. NET and popularized it on a large scale, ASP. NET gradually entered the mainstream of information system development. But at the same time, the old system developed with ASP is integrated. At this time, there is a problem: ASP and ASP. NET. NET writes Chinese COOKIES and reads them using ASP. The read results are garbled, not Chinese.

Later, I found the root cause of the problem by searching for information and continuously practicing it. The reason why Chinese COOKIES cannot be correctly read in ASP is that their Chinese encoding formats are different.

The Web. config configuration file of the development project contains the following information:

<! -- Globalization This section sets the application globalization settings. -->
<Globalization requestEncoding = "UTF-8" responseEncoding = "UTF-8"/>

We can see that, without special project configuration, Chinese COOKIES in the system are transmitted by UTF-8 encoding by default, the Chinese COOKIES in ASP are transmitted by "GB2312" encoding by default, so ASP and ASP.. NET Chinese COOKIES cannot be shared by default, but can be solved through the following methods:

Use the following method to write a COOKIE:Copy codeThe Code is as follows :'----------------------------------------------------------------
'Name: WriteCookie (ByVal strCookieName As String, ByVal strKeyName As String, ByVal strKeyValue As String)
'Parameters:
'[ByVal] strCookieName-cookie name
'[ByVal] strKeyName-key name
'[ByVal] strKeyValue-key value.
'Return: String
'----------------------------------------------------------------
Public Function WriteCookie (ByVal strCookieName As String, ByVal strKeyName As String, ByVal strKeyValue As String) As String
Dim objEnc As System. Text. Encoding = System. Text. Encoding. GetEncoding ("GB2312 ")
StrKeyValue = System. Web. HttpUtility. UrlEncode (strKeyValue, objEnc)
System. Web. HttpContext. Current. Response. Cookies (strCookieName) (strKeyName) = strKeyValue
End Function

The above method enables ASP to correctly read ASP.. NET. NET will read the Chinese COOKIES directly in the front-end display will also display garbled characters, this is because ASP. NET uses the UTF-8 encoding method to display Chinese information of the GB2312 encoding method. Therefore, if a problem occurs, you can use the following method to correctly display Chinese Information on the front-end page:

Dim uName As String = System. Web. HttpContext. Current. Server. UrlDecode (System. Web. HttpContext. Current. Request. Cookies (strCookieName) (strKeyName ))
Dim objEnc As System. Text. Encoding = System. Text. Encoding. GetEncoding ("GB2312 ")
UName = System. Web. HttpUtility. UrlDecode (uName, objEnc)

The above methods solve the problem of Chinese COOKIES in ASP and ASP.. NET. in the config configuration file, change the "UTF-8" encoding method to "GB2312" to solve the above problem. You can give it a try and tell me the result.

In ASP. NET, we usually clear COOKIES using the following methods:Copy codeThe Code is as follows: Sub CleanCookies ()
Dim I As Integer
Dim cookie As System. Web. HttpCookie
For I = 0 To System. Web. HttpContext. Current. Request. Cookies. Count-1
Cookie = System. Web. HttpContext. Current. Request. Cookies (I)
System. Web. HttpContext. Current. Response. Cookies (CStr (cookie. Name). Value = ""
System. Web. HttpContext. Current. Response. Cookies (CStr (cookie. Name). Expires = DateAdd (DateInterval. Day,-1, Now)
Next
End Sub

However, ASP will not be able to read ASP when the COOKIES are cleared and logged on again through the above methods. net cookies, but the following statement is added in the above method to solve the problem that the COOKIES cannot be cleared correctly:

System. Web. HttpContext. Current. Response. Cookies. Clear ()

The above is a summary of some of my little experiences at work, which may be inaccurate. I hope you will approve it!

Related Article

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.