PRB: XMLHttpRequest setRequestHeader Method and Cookies

ID: Q234486


The information in this article applies to:
  • Microsoft Internet Explorer (Programming) version 5


SYMPTOMS

When using the XML Document Object Model (DOM), the setRequestHeader method on the XMLHttpRequest object does not seem to set cookie headers as expected. The first call to setRequestHeader using the Cookie HTTP header seems to have no effect.


RESOLUTION

To add cookies to the request, the call to setRequestHeader for the Cookie header must be repeated because the first call is ignored:


'this value is ignored, but the step is necessary
xmlRequest.setRequestHeader "Cookie", "any non-empty string here"
'set all cookies here
xmlRequest.setRequestHeader "Cookie", "cookie1=value1; cookie2=value2" 
Note that setting cookies in this manner is atypical. Cookies are best set by the server using the Set-Cookie header.


MORE INFORMATION

Here is some example Visual Basic code that creates an XML HTTP request and sends an XML document to a Web server after setting a cookie on the request:


Dim strXML As String
Dim strURL As String
Dim objHTTPRequest As New MSXML.XMLHTTPRequest
Dim objXMLDocument As New MSXML.DOMDocument
    
strXML = "<TESTDOC>A Test XML Document</TESTDOC>"
objXMLDocument.loadXML strXML
objHTTPRequest.open "POST", "http://test.microsoft.com/xmltest.asp", False
    
objHTTPRequest.setRequestHeader "Cookie", "cookietest=testvalue"
' Must repeat call to get cookie set
objHTTPRequest.setRequestHeader "Cookie", "cookietest=testvalue"

objHTTPRequest.send objXMLDocument
    
MsgBox objHTTPRequest.responseText
    
Set objXMLDocument = Nothing
Set objHTTPRequest = Nothing 


REFERENCES

For more information on XML, see the XML documentation on the Microsoft Developer Network (MSDN).

© Microsoft Corporation 1999, All Rights Reserved.
Contributions by Jason Strayer, Microsoft Corporation

Additional query words:

Keywords : kbIE500 kbXML kbDSupport
Version : WINDOWS:5
Platform : WINDOWS
Issue type : kbprb


Last Reviewed: January 26, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.