After Microsoft launched .NET and carried out large-scale promotion and popularization, ASP.NET gradually entered the mainstream of information system development. But at the same time, the old system developed with ASP is facing being integrated. At this time, it faces a problem: when ASP and ASP.NET are integrated with each other, the Chinese COOKIES information cannot be shared with each other. When using ASP.NET to write After reading the Chinese COOKIES information using ASP, what was read was garbled characters instead of Chinese.
Later, after searching for information and practicing constantly, I finally found the root cause of the problem. The reason why Chinese COOKIES information cannot be read correctly in ASP is that its Chinese encoding format is different.
The development project Web.config configuration file contains the following information:
<!-- Globalization This section sets the globalization settings of the application. -->
<globalization requestEncoding="utf-8" responseEncoding="utf-8" />
It can be seen from this that without special configuration of the project, the Chinese COOKIES information in the system is transmitted through the "utf-8" encoding method by default. , and the Chinese COOKIES information in ASP is transmitted through the "GB2312" encoding method by default, so the Chinese COOKIES information between ASP and ASP.NET cannot be shared by each other by default, but it can be solved by the following methods:
Use the following method to write COOKIE:
'------------------------------------------------- -----------------------
' 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 the Chinese COOKIES information written by ASP.NET, but at the same time, if the Chinese COOKIES information read in ASP.NET is directly displayed in the front desk, it will be displayed as garbled characters. This is Since ASP.NET uses "utf-8" encoding to display Chinese information in "GB2312" encoding, a problem occurs. You can use the following method to correctly display Chinese information on the front 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 method solves the problem that Chinese COOKIES information cannot be shared between ASP and ASP.NET. However, I wonder if it can be modified by modifying the encoding method in the Web.config configuration file. , changing the "utf-8" encoding method to "GB2312" encoding method can solve the above problem. It should be possible. You can try it and tell me the result.
In ASP.NET we generally clear COOKIES information through the following methods:
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
But when you clear the COOKIES information through the above method and log in again, ASP will not be able to read the COOKIES of ASP.NET. The reason is unknown. However, adding the following statement to the above method of clearing COOKIES information can solve the problem of not being able to correctly clear the COOKIES information. Question:
System.Web.HttpContext.Current.Response.Cookies.Clear()
The above is a summary of some of my experiences at work. There may be inaccuracies. I hope you will correct me!