经典 ASP 的 Request.Form 会丢失 8 位字符 - 有没有简单的方法来防止这种情况发生?
我的一个客户正在使用经典 ASP 脚本来处理来自第三方支付处理器的表单(这是信用卡交易序列的最后一步,从客户的网站开始,转到第三方网站,然后返回到客户站点)。
客户位于奥地利,当其中一个字段包含 8 位字符时(例如,当字段值为 Österreich 时),当我以标准方式检索该字段的值时,会简单地删除 Ö;例如:
fieldval = Request.Form("country")
If fieldval = "sterreich" Then
' Code here will execute
End If
第三方页面正在 POST 的文字值为 %D6sterreich
,我认为表明 POST 正在以 UTF-8 编码。
POST 请求具有以下可能相关的标头:
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Content-Type: application/ x-www-form-urlencoded
我绝不是字符编码专家,这是我第一次真正使用经典 ASP 做任何事情,所以我有点困惑。
通过一些谷歌搜索和搜索,我已将以下内容添加到处理 POST 的页面中:
<%@ Codepage=65001 %>
<%
Response.CharSet = "UTF-8"
Response.Codepage = 65001
%>
但这没有任何区别 - 我仍然丢失了最初的 8 位字符。有什么非常简单的事情我只是不知道吗?
A client of mine is using a Classic ASP script to process a form from a third-party payment processor (this is the last step in a credit-card-transaction sequence that starts at the client's website, goes to the third-party site, and then returns to the client's site).
The client is in Austria and when one of the fields includes an 8-bit character (e.g., when the field value is Österreich), the Ö is simply dropped when I retrieve the value of the field in the standard way; e.g.:
fieldval = Request.Form("country")
If fieldval = "sterreich" Then
' Code here will execute
End If
The literal value that the third-party page is POSTing is %D6sterreich
, which I think suggests that the POST is being encoded in UTF-8.
The POST request has the following possibly-relevant headers:
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Content-Type: application/x-www-form-urlencoded
I'm by no means a character-encoding expert and this is the first time I've really done anything with Classic ASP, so I'm kind of flummoxed.
From some Googling and searching SO, I've added the following to the page that processes the POST:
<%@ Codepage=65001 %>
<%
Response.CharSet = "UTF-8"
Response.Codepage = 65001
%>
But it doesn't make any difference -- I still lose that initial 8-bit character. Is there something really simple that I'm just not aware of?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
尝试将以下内容添加到页面顶部:
Try adding the following to the top of the page:
事实证明我的方向是错误的。有问题的 ASP 文件本身是用 UTF-8 编码的,这隐式地将 Response.CodePage 设置为 65001 —— 换句话说,显式添加
CODEPAGE
指令没有什么区别 —— 事实上 UTF-8 编码是问题的根源。当我将文件重新编码为 Windows-1252 时,问题消失了。一般来说,我对字符编码非常无知,但我认为回想起来,POST 中的
%D6
应该是我的线索——如果我开始正确理解事情,那么单字节 < code>0xD6 不是有效的 UTF-8 字符。也许更熟悉这些事情的人可以证实或否认这一点。Turns out I was going the wrong direction with this. The ASP file in question was itself encoded in UTF-8, which was implicitly setting Response.CodePage to 65001 -- in other words, explicitly adding a
CODEPAGE
directive made no difference -- and in fact the UTF-8 encoding was the source of the problem.When I re-encoded the file to Windows-1252, the problem disappeared. I'm pretty ignorant of character encodings in general, but I think in retrospect the
%D6
in the POST should have been my clue -- if I'm starting to understand things rightly, the single byte0xD6
is not a valid UTF-8 character. Maybe someone more familiar with these things could confirm or deny this.如果在查询字符串中使用 Ascii 字符 0(编码为 (%00)),我可以检索整个值而不用 Ascii 0 终止吗?
What about using the Ascii Character 0 in the query string, encoded as (%00), can I retrieve the whole value without terminating by Ascii 0?
@Ben Dunlap:在页面顶部尝试这个 -
更新
如果您执行
Response.Write Request.Form("country")
,它会显示什么?@Ben Dunlap: Try this at the top of the page --
Update
If you do a
Response.Write Request.Form("country")
, what does it display?我使用的 2 个简单步骤是:
在每个 asp 文件的顶部添加:
Response.CharSet =“utf-8”
Response.CodePage = 65001
以“ANSI”编码(不是utf-8!) - 此选项通常位于高级文本编辑器的“保存”窗口
如果您以 utf-8 编码保存,或者如果您没有添加代码顶部指定的两行,则这将永远不会按您的预期工作。
The 2 simple steps I used were:
add at the top of EVERY asp file:
Response.CharSet = "utf-8"
Response.CodePage = 65001
save every ASP text file in "ANSI" encoding (NOT utf-8!) - this option is usually found in the "Save" window of advanced text editors
If you save in utf-8 encoding or if you don't add the two line specified at the top of your code, this will never work as you intended.
我的问题是类似的(但很奇怪),并且在我的所有页面上添加以下两行已纠正它。非常感谢。
但是,为了解释一下,这就是我遇到的确切问题。人们在我的 ASP 输入页面上输入西班牙语字符,结果非常奇怪。例如,输入了“Peña”。ASP 页面将按照输入的内容显示此内容,但最终在数据库中显示的内容却显示为“Pe?a”。这应该没什么问题,除了实际存储在的十六进制值之外。数据库是 0x50653F6100。请注意,数据库存储的值末尾有一个额外的 NULL,因此,当我后来检索数据时,当“00”[null] 被击中时,屏幕变得有点疯狂。 。
无论如何,添加这两行似乎已经解决了问题,并且“ñ”按应有的方式存储在数据库中
My issue was similar (but quite strange) and adding the following two lines on all my pages has corrected it. Thanks so much for this.
But, to explain, here is the exact issue I had. Folks were entering Spanish characters on my ASP entry page and the results were very weird. For example" "Peña" was entered. The ASP page would display this, as entered, but what ended up in the database was displayed back as "Pe?a". This would have been sort of ok, except the hex actually stored in the database was 0x50653F6100. Notice the extra "00". Somehow the database stored value had an extra NULL at the end. So, when I later retrieved the data the screens went a little bonkers when the "00" [null] was hit and the displayed data essentially stopped after this data.
In any case adding the two lines seems to have fixed the issue and the "ñ" is stored in the database as it should be.