日期:2010-08-27 浏览次数:20475 次
测试通过系统:WinXP 中文Pro, XML4.0 SP2,C#
尝试过XMLHTTP作客户端,然后尝试与服务器端ASP交互的程序员,我认为都很有思路,当然这也是在自夸:)。但最头疼的问题恐怕就是中文乱码的问题,查了很多资料,MSDN,互联网上的,尝试了很多方法都不太奏效,还好没有气馁,现在,最新的最简单的解决办法闪亮登场:
把客户端要传输的XML的头由:
<?xml version="1.0" encoding="gb2312" ?>
改为:
<?xml version="1.0" encoding="utf-8" ?>
服务器端的ASP程序发送给客户端XML结果时需要加:
Response.ContentType = "text/xml"
Response.CharSet = "gb2312"
客户端的程序取返回结果用XmlDom.loadXml(xmlhttp.responseText)就可以了。
============================================================================
以下分析可能的原因:
可能是我们的操作系统本身使用UTF-8编码的原因。
把所有Request.ServerVariables写到一个文本文件中你会发现类似这些:
ALL_HTTP:HTTP_ACCEPT:*/*
HTTP_ACCEPT_LANGUAGE:zh-cn
HTTP_CONNECTION:Keep-Alive
HTTP_HOST:localhost
HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)
HTTP_COOKIE:ASPSESSIONIDAQBCSQRA=FNEHNOCCMHECCOPIOKKECEFM
HTTP_CONTENT_LENGTH:94
HTTP_CONTENT_TYPE:text/xml;charset=gb2312
HTTP_ACCEPT_ENCODING:gzip, deflate
HTTP_CACHE_CONTROL:no-cache
ALL_RAW:Accept: */*
Accept-Language: zh-cn
Connection: Keep-Alive
Host: localhost
User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)
Cookie: ASPSESSIONIDAQBCSQRA=FNEHNOCCMHECCOPIOKKECEFM
Content-Length: 94
Content-Type: text/xml;charset=gb2312
Accept-Encoding: gzip, deflate
Cache-Control: no-cache
APPL_MD_PATH:/LM/W3SVC/1/Root/zdqs
APPL_PHYSICAL_PATH:C:\Inetpub\systems\ZDS\qry\
AUTH_PASSWORD:
AUTH_TYPE:
AUTH_USER:
CERT_COOKIE:
CERT_FLAGS:
CERT_ISSUER:
CERT_KEYSIZE:
CERT_SECRETKEYSIZE:
CERT_SERIALNUMBER:
CERT_SERVER_ISSUER:
CERT_SERVER_SUBJECT:
CERT_SUBJECT:
CONTENT_LENGTH:94
CONTENT_TYPE:text/xml;charset=gb2312
GATEWAY_INTERFACE:CGI/1.1
HTTPS:off
HTTPS_KEYSIZE:
HTTPS_SECRETKEYSIZE:
HTTPS_SERVER_ISSUER:
HTTPS_SERVER_SUBJECT:
INSTANCE_ID:1
INSTANCE_META_PATH:/LM/W3SVC/1
LOCAL_ADDR:127.0.0.1
LOGON_USER:
PATH_INFO:/zdqs/QURY.asp
PATH_TRANSLATED:C:\Inetpub\systems\ZDS\qry\QURY.asp
QUERY_STRING:
REMOTE_ADDR:127.0.0.1
REMOTE_HOST:127.0.0.1
REMOTE_USER:
REQUEST_METHOD:POST
SCRIPT_NAME:/zdqs/QURY.asp
SERVER_NAME:localhost
SERVER_PORT:80
SERVER_PORT_SECURE:0
SERVER_PROTOCOL:HTTP/1.1
SERVER_SOFTWARE:Microsoft-IIS/5.1
URL:/zdqs/QURY.asp
HTTP_ACCEPT:*/*
HTTP_ACCEPT_LANGUAGE:zh-cn
HTTP_CONNECTION:Keep-Alive
HTTP_HOST:localhost
HTTP_USER_AGENT:Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.1.4322; .NET CLR 2.0.50727; InfoPath.1)
HTTP_COOKIE:ASPSESSIONIDAQBCSQRA=FNEHNOCCMHECCOPIOKKECEFM
HTTP_CONTENT_LENGTH:94
HTTP_CONTENT_TYPE:text/xml;charset=gb2312
HTTP_ACCEPT_ENCODING:gzip, deflate
HTTP_CACHE_CONTROL:no-cache
猜测一:网络传输过程中所用的编码方式是gb2312
然后,请看另外MSXML4 SDK中一个帮助:
In some cases, an XML document is passed to and processed by an application—for example, an ASP page—that cannot properly decode rare or new characters. When this happens, you might be able to work around the problem by relying on DOM to handle the character encoding. This bypasses the incapable application.
For example, the following XML document contains the character entity ("€") that corresponds to the Euro currency symbol (€). The ASP page, incapable.asp, cannot process currency.xml.
XML Data (currency.xml)