Specifies the name of an HTTP header.
oXMLHttpRequest.setRequestHeader(bstrHeader, bstrValue);
"depth"
. This parameter should not contain a colon and should be the actual text of the HTTP header."infinity"
.
Note You must call the open
method before you call this method. Otherwise, an error will occur.
The following script example posts a DOMDocument
to an Active Server Page (ASP) on a server and returns the result as a new XML document.
HTML File (form.htm)
<HTML>
<HEAD>
<SCRIPT LANGUAGE="VBScript">
Function onLoad()
Dim mydata, pi
Set mydata = CreateObject("Msxml2.DOMDocument.5.0")
Set pi = mydata.createProcessingInstruction("xml", "version='1.0' encoding='UTF-8'")
mydata.insertBefore pi, mydata.firstChild
End function
Function sendInfo()
Dim MyHttp
'Do validation of input data before sending it.
If(Not(customerName.value = "")) then
With MyData.documentElement
.getElementsByTagName("Name").item(0).text = customerName.value
.getElementsByTagName("Phone").item(0).text = customerPhoneNum.value
End With
Set MyHttp=CreateObject("Msxml2.XMLHTTP.5.0")
MyHttp.open "POST", "http://localhost
/httpreqserver.asp", False
'Simulate message sent by a custom user agent.
MyHttp.setRequestHeader "User-Agent", "MyCustomUser"
MyData.async = False
MyHttp.send mydata.XMLDocument
Document.Write MyHttp.responseText
Else
Document.Write "Invalid data."
End If
End function
</SCRIPT>
</HEAD>
<BODY LANGUAGE="JScript" ONLOAD="Return onLoad()">
<TABLE BORDER="2" ALIGN="center">
<TR><TD WIDTH="150" ALIGN="center">
<LABEL>Name</LABEL>
</TD><TD>
<INPUT NAME="customerName" TYPE="EDIT"/>
</TD></TR>
<TR><TD WIDTH="150" align="CENTER">
<LABEL>Telephone number</LABEL>
</TD><TD>
<INPUT NAME="customerPhoneNum" type="EDIT"/>
</TD></TR>
</TABLE>
<TABLE ALIGN="CENTER">
<TR><TD WIDTH="150" ALIGN="CENTER">
<INPUT TYPE="BUTTON" VALUE="Send Information" ALIGN="CENTER" ONCLICK="sendInfo()"/>
</TD></TR>
</TABLE>
</BODY>
</HTML>
<XML id="MyData">
<MyStructure>
<Name/>
<Phone/>
</MyStructure>
</XML>
ASP File (httpreqserver.asp)
<%@LANGUAGE="Jscript"%> <% Response.Expires = -1000; // Load the posted XML document. var doc = Server.CreateObject("Msxml2.DOMDocument.5.0"); doc.async=false; doc.load(Request); var result = Server.CreateObject("Msxml2.DOMDocument.5.0"); // Now process the order and build the result document. var userAgent = Request.ServerVariables("HTTP_User-Agent"); var OutputString="Data for "+ doc.documentElement.childNodes.item(0).text + " (" + doc.documentElement.childNodes.item(1).text + ") added"; Response.ContentType = "text/xml"; if(userAgent == "MyCustomUser") { result.loadXML("<result>" + OutputString +" </result>"); var pi = result.createProcessingInstruction("xml", "version='1.0'"); result.insertBefore( pi, result.firstChild); result.save(Response); } else { Response.Write("<P><B>" + OutputString+" </B></P>"); } %>
Try It!
To run this sample, you need access to a computer running Internet Information Services (IIS) 5.0 or later.
Note If you are not running IIS locally on your computer, locate the following line:
MyHttp.open "POST", "http://
localhost
/httpreqserver.asp", False
Substitute the name of the remote computer for the characters "localhost", and save the file.
Output
When run, the sample Web application should return as output in the browser the name and phone number you entered as input. To verify that this information was generated using the XML <result>
node string from httpreqserver.asp, you can do the following:
<?xml version="1.0"?> <result>Data for [name input] ([phone number input] )added.</result>
oXMLHttpRequest.setRequestHeader(bstrHeader, bstrValue)
"depth"
. This parameter should not contain a colon and should be the actual text of the HTTP header."infinity"
.HRESULT setRequestHeader(BSTR bstrHeader, BSTR bstrValue);
"depth"
. This parameter should not contain a colon and should be the actual text of the HTTP header."infinity"
.If another header already exists with this name, it is replaced.
To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button in the upper-left corner of the page.
Applies to: IXMLHTTPRequest