尝试过XMLHTTP作客户端,然后尝试与服务器端ASP交互的程序员,我认为都很有思路,当然这也是在自夸:)。但最头疼的问题恐怕就是中文乱码的问题,查了很多资料,MSDN,互联网上的,尝试了很多方法都不太奏效,还好没有气馁,现在,最新的最简单的解决办法闪亮登场:
Enforcing Character Encoding with DOM
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方法时。
由猜想一、猜想二得出
猜想三:客户端编译的系统使用的字符串本身就是采用GB2312编码的,而使用XMLHTTP传输数据时自动转换为GB2312,服务器端用DOM对象load时由于相当于载入一个字节流,然后一看xml头中的encoding就是GB2312,所以就没做转换,直接把字节流视为字符串!!!不好意思是它的确忘记了一件事就是,这个字符串在我的系统显示时却认为是UTF-8编码的,所以只有强制xml转换以下就行了,好像见别人的解决方案时也有写gb2312到utf-8转换函数的……
最后实践,证实可行!!!