ASP模擬POST提交,然後XMLHTTP獲取資料總是亂碼,請大家幫忙,感謝!
阿新 • • 發佈:2019-02-10
目前在建的一個專案要求使用外部的一個網站達到切詞的目的,由於外部網站的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("我們的李宇春天")
<%
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顯示的時候才需要轉)
-
POST /scws/api.php HTTP/1.0
-
Accept: */*
-
Content-Type: application/x-www-form-urlencoded;charset=utf-8
-
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)
-
Host: www.ftphp.com
-
Content-Length: 97
-
Connection: Keep-Alive
-
Pragma: no-cache
-
data=我們的李宇春天&respond=xml&charset=utf8&ignore=yes&duality=yes&traditional=no&multi=5
-
HTTP/1.1 200 OK
-
Server: nginx
-
Date: Mon, 09 Apr 2012 06:12:12 GMT
-
Content-Type: text/xml; charset=utf-8
-
Connection: close
-
Vary: Accept-Encoding
-
<?xml version="1.0" encoding="utf-8"?>
-
<scws:respond xmlns:scws="http://www.ftphp.com/scws">
-
<status>ok</status>
-
<words>
-
<word><![CDATA[我們]]></word>
-
<off>0</off>
-
<len>6</len>
-
<idf>4.4200000762939</idf>
-
<attr>r</attr>
-
</words>
-
<words>
-
<word><![CDATA[的]]></word>
-
<off>6</off>
-
<len>3</len>
-
<idf>0</idf>
-
<attr>uj</attr>
-
</words>
-
<words>
-
<word><![CDATA[李宇春]]></word>
-
<off>9</off>
-
<len>9</len>
-
<idf>9.3599996566772</idf>
-
<attr>nr</attr>
-
</words>
-
<words>
-
<word><![CDATA[天]]></word>
-
<off>18</off>
-
<len>3</len>
-
<idf>0</idf>
-
<attr>n</attr>
-
</words>
-
</scws:respond>