麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 開發 > XML > 正文

關于XMLHTTP客戶端與ASP交互傳輸XML時所產生的中文編碼問題的解決方案

2024-09-05 20:56:01
字體:
來源:轉載
供稿:網友
 

測試通過系統: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中一個幫助:

 

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 ("&#8364;") 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>&#8364;</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轉換函數的……

最后實踐,證實可行!!!

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 99riav国产在线观看 | 欧美成人精品一区二区三区 | 日本网站在线播放 | 成人国产视频在线观看 | 91羞羞 | 成人午夜免费在线观看 | 免费成人 | 亚洲第一黄色网 | 国产最新网站 | 久久精品免费网站 | 国产一级αv片免费观看 | 国产88久久久国产精品免费二区 | 亚洲自拍第一 | 国产美女视频一区二区三区 | 久久久久久久免费视频 | 日韩在线激情 | 日韩精品中文字幕一区二区三区 | 成人午夜在线观看视频 | 国产va在线观看免费 | xxxx hd video 69| 成人性视频欧美一区二区三区 | 全黄性色大片 | 久久精品一区二区三区不卡牛牛 | 黄色大片网站在线观看 | 一级空姐毛片 | 欧美女人天堂 | 欧美成人免费电影 | 最新亚洲视频 | 欧美一级美国一级 | 久久99精品久久久久久国产越南 | 国产午夜电影 | 成人综合免费视频 | 国产成人77亚洲精品www | av在线影片 | 日本欧美一区二区三区在线观看 | 久久精品欧美一区二区 | 亚洲视频网 | 激情宗合| 成人福利视频导航 | 国产一区二区二 | 日韩美女电影 |