日期:2010-05-03  浏览次数:20496 次

使用HTTPService与Asp的伪Web服务通讯,完成数据库操作

我提供一个asp的伪服务思想:

clientReq.xml:
-------------------------------------------------------------------------------------------------
<?xml version="1.0" encoding="gb2312"?>
<TestPacket>
<MsgType>GetvalueReq</MsgType>
<Version>1.0</Version>
</TestPacket>


Index.mxml:
-------------------------------------------------------------------------------------------------
<mx:Model id="mReq" source="clientReq.xml"/>
<mx:HTTPService id="hs" url="http://asp/TestServer.asp"
resultFormat="xml"
method="POST"
contentType="application/xml"
showBusyCursor="true">
<mx:request>
<xml>{mReq}</xml>
</mx:request>
</mx:HTTPService>


TestServer.asp:
-------------------------------------------------------------------------------------------------
主要有获得数据包,解析数据包,指定操作,返回数据四步

1。获得提交的数据包
Set oDom = Server.CreateObject("Msxml2.DOMDocument")
oDom.Load Request

2。解析数据包,获得MsgType
sMsgType = Trim(oDom.selectSingleNode("//MsgType").text)
Select Case sMsgType
Case "GetvalueReq"
Call Getvalue
'Case "DeletevalueReq"
' Call Deletevalue
Case "InsertvalueReq"
' Call Insertvalue(oDom)
'Case "UpdatevalueReq"
' Call Updatevalue
'Else Case
' Call ShowErrMsg
End Select

3。指定操作,此处完成Getvalue操作,其中有很多可以做成公共的模块供其他函数使用
sConn = "Driver={Microsoft Access Driver (*.mdb)};DBQ=" & Server.MapPath("Message.mdb")
Set oConn = Server.CreateObject("ADODB.Connection")
oConn.Open sConn
sSql = "select * from Message order by ID desc"
Set oRs = Server.CreateObject("ADODB.RecordSet")
oRs.Open sSql, oConn, 1, 1

'构造无记录xml文档
If oRs.Eof And oRs.Bof Then
sXml = "<?xml version=""1.0"" encoding=""gb2312""?>" & vbCrLf &_
"<TestPacket>" & vbCrLf &_
" <MsgType>SystemResp</MsgType>" & vbCrLf &_
" <Version>1.0</Version>" & vbCrLf &_
" <Result>0</Result>" & vbCrLf &_
"</TestPacket>"
End If

'构造有记录xml文档
If Not (oRs.Eof And oRs.Bof) Then
Do While Not oRs.Eof
sTmpXml = sTmpXml & "<Messages>" & vbCrLf &_
"<Name>" & oRs("Name") & "</Name>" & vbCrLf &_
"<Age>" & oRs("Age") & "</Age>" & vbCrLf &_
"<Sex>" & oRs("Sex") & "</Sex>" & vbCrLf &_
"<Msg>" & oRs("Msg") & "</Msg>" & vbCrLf &_
"</Messages>" & vbCrLf
oRs.MoveNext
Loop
sXml = "<?xml version=""1.0"" encoding=""gb2312""?>" & vbCrLf &_
"<TestPacket>" & vbCrLf &_
" <MsgType>SystemResp</MsgType>" & vbCrLf &_
" <Version>1.0</Version>" & vbCrLf &_
" <Result>1</Result>" & vbCrLf & sTmpXml &_
"</TestPacket>"
End If

4.返回数据
Set oDom = Server.CreateObject("Msxml2.DOMDocument")
oDom.LoadXML(sXml)
oDom.Save(Response)
Set oDom = Nothing


Message.mdb
-------------------------------------------------------------------------------------------------
数据库名:Message.mdb
数据表名:Message
数据字段:字段 类型
ID 自动编号
Name 文本
Age 数字
Sex 文本
Msg 备注


执行结果:
----