I'm adding logging functionality to a Classic ASP application and am having difficulty getting Request.QueryString and Request.ServerVariables to display correctly when users submit UNICODE queries.
For example -
Response.write Request.QueryString
strVar1=&strVar2=%E8%A1%8C%E9%9B%B2%E6%B5%81%E6%B0%B4&strVar3=blah1&strVar4=blah2
Response.write Request.ServerVariables("QUERY_STRING")
strVar1=&strVar2=%E8%A1%8C%E9%9B%B2%E6%B5%81%E6%B0%B4&strVar3=blah1&strVar4=blah2
Yet if I specify a UNICODE variable in Request.QueryString it prints correctly:
Response.write Request.QueryString("strVar2")
行雲流水
How can I get Request.QueryString or Request.ServerVariables("QUERY_STRING") to include UNICODE? I do have the following on both my search and results pages and queries execute successfully against the database:
<%
Response.CodePage = 65001
Response.CharSet = "utf-8"
%>
To answer bobince's question, I'm trying to log search terms and pages from which they're submitted - if there's a better way to do this I'm all ears:
'Enable Logging: writes to MyDB.dbo.logging
'---------------------------------------------------------------------------------------------
Set cmd = Server.CreateObject("ADODB.Command")
cmd.CommandType = adCmdText
cmd.ActiveConnection = objConn
strADName = UCase(Request.ServerVariables("AUTH_USER"))
Protocol = Request.ServerVariables("SERVER_PROTOCOL")
Protocol = Left(Protocol,InStr(Request.ServerVariables("SERVER_PROTOCOL"),"/")-1)
if Request.ServerVariables("SERVER_PORT") = "80" then
port = ""
else
port = ":" & Request.ServerVariables("SERVER_PORT")
end if
CurPageURL = lcase(Protocol) & "://" & Request.ServerVariables("SERVER_NAME") &_
port & Request.ServerVariables("SCRIPT_NAME") & "?" & _
Request.ServerVariables("QUERY_STRING")
strSQL = "INSERT INTO MyDB.dbo.Logging([user],URL) SELECT ?, ? "
cmd.Parameters.Append (cmd.CreateParameter("User", adVarWChar, adParamInput, len(strADName), strADName))
cmd.Parameters.Append (cmd.CreateParameter("URL", adVarWChar, adParamInput, len(CurPageURL), CurPageURL))
cmd.CommandText = strSQL
set objRS = cmd.Execute
'-----------------------------------------------------------------------------------------------
This isn't really anything to do with Unicode.
Request.QueryString does two separate things.
If you use it without arguments, it returns the whole query string exactly as submitted by the browser (as above, the same as the QUERY_STRING server variable).
If you use it with an argument like "strVar2", it splits up the query string into parameter parts, finds the one(s) that correspond to the argument name (strVar2=...), and returns the value. In doing so it takes care of URL-decoding the components of the query string including the name and the value, so the %xx sequences in the input are decoded to the byte sequence they represent: 0xE8, 0xA1, 0x8C and so on. When you print that byte string to a page that a browser decodes as UTF-8, they will see 行雲流水.
You can do a URL-decode step yourself on the full query string if you really want. There isn't a built-in for URL-decoding in Classic ASP but you can write such a function yourself (see eg URLDecode from http://www.aspnut.com/reference/encoding.asp), or use
decodeURIComponent(s.replace(/\+/g, ' '))
from JScript.
Note that if you URL-decode a whole URL string together you are kind of breaking it. For example for input query string:
foo=abc%26def&bar=%e6%97%a5%e6%9c%ac%e8%aa%9e
you would get:
foo=abc&def&bar=日本語
which is fine for the Japanese, but the ampersand in the value for foo has broken the whole string so you can no longer tell for sure what the original parameters were. You should generally only decode URL components once you have split them up from the URL they came in. This is what ASP does for you: Request.QueryString("foo") would correctly return abc&def here which is why you should almost always be using that method to retrieve parameters.
What exactly are you trying to do by decoding a whole query string?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With