|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
前几天同学问我学习方向的问题。有点想法,不知道对不对,怕误导同学,现在“开源一下”。注:括号内是我现在整理的时填加上的。xml|编码|交互|办理|客户端|成绩|中文测试经由过程体系:WinXP中文Pro,XML4.0SP2,C#
实验过XMLHTTP作客户端,然后实验与服务器端ASP交互的程序员,我以为都很有思绪,固然这也是在自诩:)。但最头疼的成绩生怕就是中文乱码的成绩,查了良多材料,MSDN,互联网上的,实验了良多办法都不太见效,还好没有泄气,如今,最新的最复杂的办理举措闪亮退场:
把客户端要传输的XML的头由:
<?xmlversion="1.0"encoding="gb2312"?>
改成:
<?xmlversion="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;MSIE6.0;WindowsNT5.1;SV1;.NETCLR1.1.4322;.NETCLR2.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;MSIE6.0;WindowsNT5.1;SV1;.NETCLR1.1.4322;.NETCLR2.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:InetpubsystemsDSqry
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:InetpubsystemsDSqryQURY.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;MSIE6.0;WindowsNT5.1;SV1;.NETCLR1.1.4322;.NETCLR2.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
然后,请看别的MSXML4SDK中一个匡助:
EnforcingCharacterEncodingwithDOM
Insomecases,anXMLdocumentispassedtoandprocessedbyanapplication―forexample,anASPpage―thatcannotproperlydecoderareornewcharacters.Whenthishappens,youmightbeabletoworkaroundtheproblembyrelyingonDOMtohandlethecharacterencoding.Thisbypassestheincapableapplication.
Forexample,thefollowingXMLdocumentcontainsthecharacterentity("€")thatcorrespondstotheEurocurrencysymbol().TheASPpage,incapable.asp,cannotprocesscurrency.xml.
XMLData(currency.xml)- <?xmlversion="1.0"encoding="utf-8"?><currency><name>Euro</name><symbol>€</symbol><exchange><base>US___FCKpd___0lt;/base><rate>1.106</rate></exchange></currency>
复制代码 ASPPage(incapable.asp)- <%@language="javascript"%><%vardoc=newActiveXObject("Msxml2.DOMDocument.4.0");doc.async=false;if(doc.load(Server.MapPath("currency.xml"))==true){Response.ContentType="text/xml";Response.Write(doc.xml);}%>
复制代码 Whenincapable.aspisopenedfromaWebbrowser,anerrorsuchasthefollowingresults:- Aninvalidcharacterwasfoundintextcontent.Errorprocessingresourcehttp://MyWebServer/MyVirtualDirectory/incapable.asp.Line4,Position10
复制代码 ThiserroriscausedbytheuseoftheResponse.Write(doc.xml)instructionintheincapable.aspcode.BecauseitcallsuponASPtoencode/decodetheEurocurrencysymbolcharacterfoundincurrency.xml,itfails.
However,youcanfixthiserror.Todoso,replacethisResponse.Write(doc.xml)instructioninincapable.aspwiththefollowingline:Withthisline,theerrordoesnotoccur.TheASPcodedoesproducethecorrectoutputinaWebbrowser,asfollows:- <?xmlversion="1.0"encoding="utf-8"?><currency><name>[b]Euro[/b]</name><symbol></symbol><exchange><base>[b]US$[/b]</base><rate>[b]1.106[/b]</rate></exchange></currency>
复制代码 TheeffectofthechangeintheASPpageistolettheDOMobject(doc)―insteadoftheResponseobjectontheASPpage―handlethecharacterencoding.
请看最初一句:上例中ASP的改动在于让DOM工具(doc)――而不是ASP中的Response工具――处置字符编码。
以是得出:
料想二:你能够视Request或Response工具为一个文件句柄,假如是用DOM工具的load与save办法时。
由料想1、料想二得出
料想三:客户端编译的体系利用的字符串自己就是接纳GB2312编码的,而利用XMLHTTP传输数据时主动转换为GB2312,服务器端用DOM工具load时因为相称于载进一个字撙节,然后一看xml头中的encoding就是GB2312,以是就没做转换,间接把字撙节视为字符串!!!欠好意义是它切实其实健忘了一件事就是,这个字符串在我的体系显现时却以为是UTF-8编码的,以是只要强迫xml转换以下就好了,仿佛见他人的办理计划时也有写gb2312到utf-8转换函数的……
最初理论,证明可行!!!
用一句话归纳综合就是,客户端发送给服务器的XML,encoding全体为utf-8编码的;服务器发送给客户端,全体指定编码为:gb2312,统统OK。
以前很热炒跨平台,主要是由于硅谷挑战微软霸主地位的热情,但是冷静下来后,跨平台往往不是那么一回事。假设你有个软件,所谓的跨平台,你只需要为第二个平台上重新编译一次就行了,这样很难么? |
|