利用ASP发送和接收XML数据的处理方法

网络整理 - 09-07

因为要做移动梦网WAP的一些接口,所以要用到这种方式,接下来会有ASP.net版本的,这个是ASP版本的,利用了MSXML2.XMLHTTP对像。

以下为引用的内容:

request.asp

dim Https
set Https=server.createobject("MSXML2.XMLHTTP")
'定义一个XMLHTTP对像
Https.open "POST","",false
Https.send "<?xml version=""1.0""?><misc_command version=""1.6""><command_name>echo</command_name>
<command_data_block><sid>123456</sid><service_id>987654</service_id>
<sp_id>11111</sp_id><sp_password>22222</sp_password></command_data_block></misc_command>"
if Https.readystate=4 then
 response.write "提交成功"
 'readstate读取状态为4则成功,继续后面的,不成功当然就不用继续处理了
 dim objstream
 set objstream = Server.CreateObject("adodb.stream")
 '定义一个stream,因为读过来的直接拿出来是乱码的,所以得处理一下
 objstream.Type = 1
 objstream.Mode =3
 objstream.Open
 objstream.Write Https.responseBody
 objstream.Position = 0
 objstream.Type = 2
 objstream.Charset = "GB2312"
 html = objstream.ReadText
 '转好码,就放到html里,好关闭这些对像
 objstream.Close
 set objstream = nothing
 set https=nothing
end if
response.write html

response.asp

'创建DOMDocument对象
Set xml = Server.CreateObject ("msxml2.DOMDocument")
xml.async = False

'装载POST数据
xml.Load Request
If xml.parseError.errorCode <> 0 Then
 response.write "不能正确接收数据" & "Description: " & xml.parseError.reason & "<br>Line: " & xml.parseError.Line
End If

set blogchild=xml.getElementsByTagName("misc_command")
'the_text=blogchild.item(0).childnodes(1).text
'the_text=blogchild.item(0).text
'for i=0 to blogchild.length-1
response.write the_text

利用这种方法,ASP里调用Servlet或Web Service都是很轻松的!