XML Http 详解
Property:
1:onReadStateChange[指定当readyState属性改变时的事件处理句柄。只写]
Example: var xmlhttp=null; function PostOrder(xmldoc) { var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.5.0"); xmlhttp.Open("POST", "http://myserver/orders/processorder.asp";, false); xmlhttp.onreadystatechange= HandleStateChange; xmlhttp.Send(xmldoc); myButton.disabled = true; } function HandleStateChange() { if (xmlhttp.readyState == 4) { myButton.disabled = false; alert("Result = " + xmlhttp.responseXML.xml); } } |
2:Property:readyState[返回当前请求的状态,只读.
Example:
0 (未初始化) 对象已建立,但是尚未初始化(尚未调用open方法) 1 (初始化) 对象已建立,尚未调用send方法 2 (发送数据) send方法已调用,但是当前的状态及http头未知 3 (数据传送中) 已接收部分数据,因为响应及http头不全,这时通过responseBody和responseText获取部分数据会出现错误, 4 (完成) 数据接收完毕,此时可以通过通过responseBody和responseText获取完整的回应数据 |
3:Property:responseBody[将回应信息正文以unsigned byte数组形式返回.只读]
Example:
var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0"); xmlhttp.open("GET", "http://localhost/books.xml";, false); xmlhttp.send(); alert(xmlhttp.responseBody); |
4:Property:responseStream[以Ado Stream对象的形式返回响应信息。只读]
5:Property:
responseText 将响应信息作为字符串返回.只读
responseXML 将响应信息格式化为Xml Document对象并返回,只读
6:Property:status[返回当前请求的http状态码.只读]
长整形标准http状态码,定义如下: Number Descrīption
100 Continue 101 Switching protocols 200 OK 201 Created 202 Accepted 203 Non-Authoritative Information 204 No Content 205 Reset Content 206 Partial Content 300 Multiple Choices 301 Moved Permanently 302 Found 303 See Other 304 Not Modified 305 Use Proxy 307 Temporary Redirect 400 Bad Request 401 Unauthorized 402 Payment Required 403 Forbidden 404 Not Found 405 Method Not Allowed 406 Not Acceptable 407 Proxy Authentication Required 408 Request Timeout 409 Conflict 410 Gone 411 Length Required 412 Precondition Failed 413 Request Entity Too Large 414 Request-URI Too Long 415 Unsupported Media Type 416 Requested Range Not Suitable 417 Expectation Failed 500 Internal Server Error 501 Not Implemented 502 Bad Gateway 503 Service Unavailable 504 Gateway Timeout 505 HTTP Version Not Supported |
method:abort 取消当前请求
method:getAllResponseHeaders 获取响应的所有http头
Example var xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0"); xmlhttp.open("GET", "http://localhost/sample.xml";, false); xmlhttp.send(); alert(xmlhttp.getAllResponseHeaders()); |
getResponseHeader 从响应信息中获取指定的http头
Example var xmlhttp = new ActiveXObject("MSXML2.XMLHTTP.3.0"); xmlhttp.open("GET", "http://localhost/sample.xml";, false); xmlhttp.send(); alert(xmlhttp.getResponseHeader("Server")); |
method:open 创建一个新的http请求,并指定此请求的方法、URL以及验证信息(用户名/密码)
open 创建一个新的http请求,并指定此请求的方法、URL以及验证信息
语法 bstrUrl varAsync[可选] bstrUser[可选] bstrPassword[可选] Example |
method:send 发送请求到http服务器并接收回应
语法
oXMLHttpRequest.send(varBody); |
参数
varBody
欲通过此请求发送的数据。
Example xmlhttp = new ActiveXObject("Msxml2.XMLHTTP.3.0"); xmlhttp.open("GET", "http://localhost/sample.xml";, false); xmlhttp.send(); alert(xmlhttp.responseXML.xml); |
备注
此方法的同步或异步方式取决于open方法中的bAsync参数,如果bAsync == False,此方法将会等待请求完成或者超时时才会返回,如果bAsync == True,此方法将立即返回。
This method takes one optional parameter, which is the requestBody to use. The acceptable VARIANT input types are BSTR, SAFEARRAY of UI1 (unsigned bytes), IDispatch to an XML Document Object Model (DOM) object, and IStream *. You can use only chunked encoding (for sending) when sending IStream * input types. The component automatically sets the Content-Length header for all but IStream * input types.
如果发送的数据为BSTR,则回应被编码为utf-8, 必须在适当位置设置一个包含charset的文档类型头。
If the input type is a SAFEARRAY of UI1, the response is sent as is without additional encoding. The caller must set a Content-Type header with the appropriate content type.
如果发送的数据为XML DOM object,则回应将被编码为在xml文档中声明的编码,如果在xml文档中没有声明编码,则使用默认的UTF-8。
If the input type is an IStream *, the response is sent as is without additional encoding. The caller must set a Content-Type header with the appropriate content type.
[/CODE]
实例:
<%
Dim objXMLHTTP, xml Set xml = Server.CreateObject("Microsoft.XMLHTTP") xml.Open "GET", "http://www.codetoad.com/";;, False ' Pull the data from the web page xml.Send |
Response.write "Here's the html we now have in our xml object"
Response.write "<BR><BR><BR>"
Response.Write "<xmp>"
Response.Write xml.responseText
Response.Write "</xmp>"
Response.write "<BR><BR><BR>"
Response.write " Now here's how the page looks:<BR><BR>"
Response.Write xml.responseText
Set xml = Nothing
%>
下面是另一个实例
<%
dim objHTTP , objXML , objXSL
set ōbjHTTP = Server.CreateObject("Microsoft.XMLHTTP")
objHTTP.open "GET", "http://p.moreover.com/cgi-local/page?c=Pop%20music%20reviews&o=xml";;, false
objHTTP.send
set ōbjXML = objHTTP.responseXML
set ōbjXSL=Server.CreateObject("microsoft.xmldom")
objXSL.async=false
objXSL.load(Server.MapPath("style.xsl"))
if (objXSL.parseError.errorCode = 0) then
Response.Write(objXML.transformnode(objXSL))
else
Response.Write "Error: " & objXSL.parseError.reason & " URL:" & objXSL.url
end if
Set ōbjHTTP = Nothing
Set ōbjXML = Nothing
Set ōbjXSL = Nothing
%>
style.xsl:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl";;>
<xsl:template match="/">
<html>
<head>
<TITLE>moreover...</TITLE>
</head>
<body BGCOLOR="ffffff">
<DIV ALIGN="center">
<TABLE BGCOLOR="ffffff" BORDER="0" CELLPADDING="4" CELLSPACING="0" WIDTH="100%">
<xsl:for-each select="moreovernews/article">
<TR VALIGN="middle">
<TD ALIGN="left" BGCOLOR="ffffff">
<xsl:attribute name="HREF">
<xsl:value-of select="url" />
</xsl:attribute>
<xsl:attribute name="TARGET">
_blank
</xsl:attribute>
<xsl:value-of select="headline_text" />
<xsl:attribute name="HREF">
<xsl:value-of select="document_url" />
</xsl:attribute>
<xsl:attribute name="TARGET">
_blank
</xsl:attribute>
<xsl:value-of select="source" />
<xsl:attribute name="HREF">
<xsl:value-of select="access_registration" />
</xsl:attribute>
<xsl:attribute name="TARGET">
_blank
</xsl:attribute>
<xsl:value-of select="access_status" />
<xsl:value-of select="harvest_time" /> GMT
</TD>
</TR>
</xsl:for-each>
</TABLE>
</DIV>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
<%
Function bytes2BSTR(vIn)
dim strReturn
dim i,ThisCharCode,NextCharCode
strReturn = ""
For i = 1 To LenB(vIn)
ThisCharCode = AscB(MidB(vIn,i,1))
If ThisCharCode < &H80 Then
strReturn = strReturn & Chr(ThisCharCode)
Else
NextCharCode = AscB(MidB(vIn,i+1,1))
strReturn = strReturn & Chr(CLng(ThisCharCode) * &H100 + CInt(NextCharCode))
i = i + 1
End If
Next
bytes2BSTR = strReturn
End Function
'++++++++++++++++++++++++++++++++++++++++++
'利用xmlHttp:抓取我的详细资料
'++++++++++++++++++++++++++++++++++++++++++
Dim RobotInfo,LngStr
Set RobotInfo=Server.CreateObject("Microsoft.XMLHTTP")
RobotInfo.Open "get","../a/a.asp?B=230&ID=2446597";,false
RobotInfo.send()
IF RobotInfo.readyState<> 4 Then
Response.Write("请求失败")
Response.End()
End If
LngStr=bytes2BSTR(RobotInfo.responseBody)
Response.Write(LngStr)
%>
MSXML中提供了Microsoft.XMLHTTP对象,能够完成从数据包到Request对象的转换以及发送任务。
创建XMLHTTP对象的语句如下:
Set ōbjXML = CreateObject("Msxml2.XMLHTTP") 或
Set ōbjXML = CreateObject(“Microsoft.XMLHTTP”)
' Or, for version 3.0 of XMLHTTP, use:
' Set xml = Server.CreateObject("MSXML2.ServerXMLHTTP")
对象创建后调用Open方法对Request对象进行初始化,语法格式为:
poster.open http-method, url, async, userID, password
Open方法中包含了5个参数,前三个是必要的,后两个是可选的(在服务器需要进行身份验证时提供)。参数的含义如下所示:
http-method: HTTP的通信方式,比如GET或是 POST
url: 接收XML数据的服务器的URL地址。通常在URL中要指明 ASP或CGI程序
async: 一个布尔标识,说明请求是否为异步的。如果是异步通信方式(true),客户机就不等待服务器的响应;如果是同步方式(false),客户机就要等到服务器返回消息后才去执行其他操作
userID 用户ID,用于服务器身份验证
password 用户密码,用于服务器身份验证
XMLHTTP对象的Send方法
用Open方法对Request对象进行初始化后,调用Send方法发送XML数据:
poster.send XML-data
Send方法的参数类型是Variant,可以是字符串、DOM树或任意数据流。发送数据的方式分为同步和异步两种。在异步方式下,数据包一旦发送完毕,就结束Send进程,客户机执行其他的操作;而在同步方式下,客户机要等到服务器返回确认消息后才结束Send进程。
XMLHTTP对象中的readyState属性能够反映出服务器在处理请求时的进展状况。客户机的程序可以根据这个状态信息设置相应的事件处理方法。属性值及其含义如下表所示:
值 说明
0 Response对象已经创建,但XML文档上载过程尚未结束
1 XML文档已经装载完毕
2 XML文档已经装载完毕,正在处理中
3 部分XML文档已经解析
4 文档已经解析完毕,客户端可以接受返回消息
客户机处理响应信息
客户机接收到返回消息后,进行简单的处理,基本上就完成了C/S之间的一个交互周期。客户机接收响应是通过XMLHTTP对象的属性实现的:
● responseTxt:将返回消息作为文本字符串;
● responseXML:将返回消息视为XML文档,在服务器响应消息中含有XML数据时使用;
● responseStream:将返回消息视为Stream对象。
下面的xml文件是动态生成的最后用xmlHTTP传送出去,这是一个在客户端Javascrīpt脚本里的内容,当然你也可以写在服务器,但是要相应的改一些东西:(仅供大家参考,了解它的用法)
var xmlDoc=new ActiveXObject("MSXML2.DOMDocument");
flag=xmlDoc.loadXML("");
newNode =xmlDoc.createElement("编码")
MarkNode=xmlDoc.documentElement.appendChild(newNode);
newNode =xmlDoc.createElement("StartMark")
newNode.text=StartMark;
MarkNode.appendChild(newNode)
newNode =xmlDoc.createElement("EndMark")
newNode.text=EndMark;
MarkNode.appendChild(newNode)
newNode =xmlDoc.createElement("日期")
DateNode=xmlDoc.documentElement.appendChild(newNode);
newNode =xmlDoc.createElement("StartDate");
newNode.text=StartDate;
DateNode.appendChild(newNode)
newNode =xmlDoc.createElement("EndDate")
newNode.text=EndDate;
DateNode.appendChild(newNode);
newNode =xmlDoc.createElement("数量")
SLNode =xmlDoc.documentElement.appendChild(newNode);
newNode =xmlDoc.createElement("StartSL")
newNode.text=StartShuL
SLNode.appendChild(newNode)
newNode =xmlDoc.createElement("EndSL");
newNode.text=EndShuL
SLNode.appendChild(newNode);
newNode =xmlDoc.createElement("单价")
DJNode =xmlDoc.documentElement.appendChild(newNode)
newNode =xmlDoc.createElement("StartDJ")
newNode.text=StartDanJ;
DJNode.appendChild(newNode);
newNode =xmlDoc.createElement("EndDJ")
newNode.text=EndDanJ;
DJNode.appendChild(newNode);
newNode =xmlDoc.createElement("金额")
JENode =xmlDoc.documentElement.appendChild(newNode)
newNode =xmlDoc.createElement("StartJE")
newNode.text=StartJinE
JENode.appendChild(newNode)
newNode =xmlDoc.createElement("EndJE")
newNode.text=EndJinE
JENode.appendChild(newNode)
newNode =xmlDoc.createElement("仓库代码")
newNode.text=CK;
xmlDoc.documentElement.appendChild(newNode)
newNode =xmlDoc.createElement("票号")
newNode.text=RKPH;
xmlDoc.documentElement.appendChild(newNode)
newNode =xmlDoc.createElement("单位代码")
newNode.text=CorpName;
xmlDoc.documentElement.appendChild(newNode)
newNode =xmlDoc.createElement("BiaoShi")
newNode.text=Biaoshi
xmlDoc.documentElement.appendChild(newNode)
newNode =xmlDoc.createElement("FindCate")
newNode.text=FindCate
xmlDoc.documentElement.appendChild(newNode)
var xh =new ActiveXObject("MSXML2.XMLHTTP")
xh.open("POST","Find.asp",false)
xh.setRequestHeader("Content-Type","text/xml")
xh.setRequestHeader("Content-Type","gb2312")
xh.send(xmlDoc);
我的每一个newNode的text值是一个变量,也就是我客户端form 中input的值,