1. 程式人生 > >ASP模擬POST提交,然後XMLHTTP獲取資料總是亂碼,請大家幫忙,感謝!

ASP模擬POST提交,然後XMLHTTP獲取資料總是亂碼,請大家幫忙,感謝!

目前在建的一個專案要求使用外部的一個網站達到切詞的目的,由於外部網站的API介面要求必須是POST提交資料,因此只能模擬POST提交,然後再去讀取提交後的資料,我用以下的程式碼,獲取回來的中文總是亂碼,英文和數字沒有問題,請大家幫忙看看,非常感謝!

<%

On error resume next 

Function GetBody(ips) 

Set https = Server.CreateObject("MSXML2.XMLHTTP") 

With https 

.Open "Post", "http://www.ftphp.com/scws/api.php", False

.setRequestHeader "Content-Type","application/x-www-form-urlencoded;charset=utf-8"


.Send "data="&ips&"&respond=xml&charset=utf8&ignore=yes&duality=yes&traditional=no&multi=5"

GetBody = .ResponseBody

End With 

GetBody = BytesToBstr(GetBody,"utf-8")

Set https = Nothing 

End Function

Function BytesToBstr(body,Cset) 

dim objstream

set objstream = Server.CreateObject("adodb.stream")


objstream.Type = 1

objstream.Mode =3

objstream.Open

objstream.Write body

objstream.Position = 0

objstream.Type = 2

objstream.Charset = Cset

BytesToBstr = objstream.ReadText 

objstream.Close

set objstream = nothing

End Function

Response.Write GetBody("我們的李宇春天")

%>

是中文的且沒有任何問題,
你發的程式碼,可以正確請求,並得到內容,且中文可以正常顯示. ..




檔案儲存成UTF-8編碼.(儲存時可以用計事本另存為,編碼選擇UTF-8即可)
儲存的UTF-8編碼的檔案,需要 BOM 頭(IIS需要BOM頭才能確定它是UTF-8編碼的檔案,我在XP下測試的,如果沒有BOM頭,則會顯示ASP錯誤,因為IIS把它當成GB2312來解析了,然後那些UTF-8編碼的漢字就會導致解析出錯).


 






伺服器互動時傳送的HTTP互動的完整請求(對方返回的內容是UTF-8編碼的內容,所以在ASP獲取以後不需要轉碼,如果你需要以GB2312顯示的時候才需要轉)

  1. POST /scws/api.php HTTP/1.0
  2. Accept: */*
  3. Content-Type: application/x-www-form-urlencoded;charset=utf-8
  4. User-Agent: Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 2.0.50727; InfoPath.2; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E)
  5. Host: www.ftphp.com
  6. Content-Length: 97
  7. Connection: Keep-Alive
  8. Pragma: no-cache
  9. data=我們的李宇春天&respond=xml&charset=utf8&ignore=yes&duality=yes&traditional=no&multi=5
  10. HTTP/1.1 200 OK
  11. Server: nginx
  12. Date: Mon, 09 Apr 2012 06:12:12 GMT
  13. Content-Type: text/xml; charset=utf-8
  14. Connection: close
  15. Vary: Accept-Encoding
  16. <?xml version="1.0" encoding="utf-8"?>
  17. <scws:respond xmlns:scws="http://www.ftphp.com/scws">
  18.         <status>ok</status>
  19.         <words>
  20.                 <word><![CDATA[我們]]></word>
  21.                 <off>0</off>
  22.                 <len>6</len>
  23.                 <idf>4.4200000762939</idf>
  24.                 <attr>r</attr>
  25.         </words>
  26.         <words>
  27.                 <word><![CDATA[的]]></word>
  28.                 <off>6</off>
  29.                 <len>3</len>
  30.                 <idf>0</idf>
  31.                 <attr>uj</attr>
  32.         </words>
  33.         <words>
  34.                 <word><![CDATA[李宇春]]></word>
  35.                 <off>9</off>
  36.                 <len>9</len>
  37.                 <idf>9.3599996566772</idf>
  38.                 <attr>nr</attr>
  39.         </words>
  40.         <words>
  41.                 <word><![CDATA[天]]></word>
  42.                 <off>18</off>
  43.                 <len>3</len>
  44.                 <idf>0</idf>
  45.                 <attr>n</attr>
  46.         </words>
  47. </scws:respond>
複製程式碼