setRequestHeader Method (IXMLHTTPRequest)

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Reference

setRequestHeader Method (IXMLHTTPRequest)

Specifies the name of an HTTP header.

[Script]

Script Syntax

oXMLHttpRequest.setRequestHeader(bstrHeader, bstrValue);

Parameters

bstrHeader
A string. A header name to set; for example, "depth". This parameter should not contain a colon and should be the actual text of the HTTP header.
bstrValue
A string. The value of the header; for example, "infinity".
Note   You must call the open method before you call this method. Otherwise, an error will occur.

Example

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.

  1. Copy the HTML code provided above, and paste it into Notepad.
  2. Save the file a form.htm to a valid Web virtual directory, such as C:\Inetpub\wwwroot, on your Web server computer.
    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.
  3. Copy the ASP code provided above, and paste it into Notepad.
  4. Save the file as httpreqserver.asp, in the same Web virtual directory you used in Step 1.
  5. Open your browser to the Web URL location where you saved the sample HTML file, such as http://localhost/form.htm.
  6. Enter the name and phone number information, and click Send Information to add/submit the XML to the ASP request server page.

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:

  1. From within Internet Explorer, click View, and then click Source.
  2. You can observe that the source matches the string that was generated by the ASP request server page in this format:
    <?xml version="1.0"?>
    <result>Data for [name input] ([phone number input] )added.</result>

[Visual Basic]

Visual Basic Syntax

oXMLHttpRequest.setRequestHeader(bstrHeader, bstrValue)

Parameters

bstrHeader
A string. A header name to set; for example, "depth". This parameter should not contain a colon and should be the actual text of the HTTP header.
bstrValue
A string. The value of the header; for example, "infinity".
[C/C++]

C/C++ Syntax

HRESULT setRequestHeader(BSTR bstrHeader, BSTR bstrValue);

Parameters

bstrHeader [in]
A header name to set; for example, "depth". This parameter should not contain a colon and should be the actual text of the HTTP header.
bstrValue [in]
The value of the header; for example, "infinity".

C/C++ Return Values

S_OK
The value returned if successful.

Remarks

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 Language Filter in the upper-left corner of the page.

See Also

getResponseHeader Method

Applies to: IXMLHTTPRequest