測試通過系統(tǒng):winxp 中文pro, xml4.0 sp2,c#
嘗試過xmlhttp作客戶端,然后嘗試與服務(wù)器端asp交互的程序員,我認(rèn)為都很有思路,當(dāng)然這也是在自夸:)。但最頭疼的問題恐怕就是中文亂碼的問題,查了很多資料,msdn,互聯(lián)網(wǎng)上的,嘗試了很多方法都不太奏效,還好沒有氣餒,現(xiàn)在,最新的最簡單的解決辦法閃亮登場:
把客戶端要傳輸?shù)膞ml的頭由:
<?xml version="1.0" encoding="gb2312" ?>
改為:
<?xml version="1.0" encoding="utf-8" ?>
服務(wù)器端的asp程序發(fā)送給客戶端xml結(jié)果時需要加:
response.contenttype = "text/xml"
response.charset = "gb2312"
客戶端的程序取返回結(jié)果用xmldom.loadxml(xmlhttp.responsetext)就可以了。
============================================================================
以下分析可能的原因:
可能是我們的操作系統(tǒng)本身使用utf-8編碼的原因。
把所有request.servervariables寫到一個文本文件中你會發(fā)現(xiàn)類似這些:
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
猜測一:網(wǎng)絡(luò)傳輸過程中所用的編碼方式是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)
<?xml version="1.0" encoding="utf-8"?><currency> <name>euro</name> <symbol>€</symbol> <exchange> <base>us___fckpd___0lt;/base> <rate>1.106</rate> </exchange></currency>
asp page (incapable.asp)
<%@language = "javascript"%><% var doc = new activexobject("msxml2.domdocument.4.0"); doc.async = false; if (doc.load(server.mappath("currency.xml"))==true) { response.contenttype = "text/xml"; response.write(doc.xml); }%>when incapable.asp is opened from a web browser, an error such as the following results:
an invalid character was found in text content. error processing resource 'http://mywebserver/myvirtualdirectory/incapable.asp'. line 4, position 10
this error is caused by the use of the response.write(doc.xml) instruction in the incapable.asp code. because it calls upon asp to encode/decode the euro currency symbol character found in currency.xml, it fails.
however, you can fix this error. to do so, replace this response.write(doc.xml) instruction in incapable.asp with the following line:
doc.save(response);
with this line, the error does not occur. the asp code does produce the correct output in a web browser, as follows:
<?xml version="1.0" encoding="utf-8" ?> <currency> <name>euro</name> <symbol>?</symbol> <exchange> <base>us$</base> <rate>1.106</rate> </exchange> </currency>
the effect of the change in the asp page is to let the dom object (doc)—instead of the response object on the asp page—handle the character encoding.
請看最后一句:上例中asp的改變在于讓dom對象(doc)——而不是asp中的response對象——處理字符編碼。
所以得出:
猜想二:你可以視request或response對象為一個文件句柄,如果是用dom對象的load與save方法時。
由猜想一、猜想二得出
猜想三:客戶端編譯的系統(tǒng)使用的字符串本身就是采用gb2312編碼的,而使用xmlhttp傳輸數(shù)據(jù)時自動轉(zhuǎn)換為gb2312,服務(wù)器端用dom對象load時由于相當(dāng)于載入一個字節(jié)流,然后一看xml頭中的encoding就是gb2312,所以就沒做轉(zhuǎn)換,直接把字節(jié)流視為字符串?。。〔缓靡馑际撬拇_忘記了一件事就是,這個字符串在我的系統(tǒng)顯示時卻認(rèn)為是utf-8編碼的,所以只有強制xml轉(zhuǎn)換以下就行了,好像見別人的解決方案時也有寫gb2312到utf-8轉(zhuǎn)換函數(shù)的……
最后實踐,證實可行?。?!
新聞熱點
疑難解答