1

Problem: I am getting the following error output from If Err.Number <> 0 Then check;

Err.Number :-1072954818  
Err.Source :msxml6.dll  
Err.Source :This method cannot be called until the open method has been called.

Code:

dim objHttpRequest
dim gw_menu_request
dim HTTPMethod

HTTPMethod="POST"
Set objHttpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")    
gw_menu_request = "http://test.com?q=headerexpose/expose_headers/expose_json"       
objHttpRequest.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" 
objHttpRequest.setRequestHeader "Content-Length", 0
objHttpRequest.open HTTPMethod, gw_menu_request, false          
Response.write(objHttpRequest.ResponseXML)

If Err.Number <> 0 Then 
  Response.Write "Err.Number :" & Err.Number  & "<br/>"
  Response.Write "Err.Source :" & Err.Source & "<br/>"
  Response.Write "Err.Source :" & Err.Description  & "<br/>"
  Response.Write "Err.File :" & Err.File & "<br/>"
End If

What am I missing here?

user692942
  • 16,398
  • 7
  • 76
  • 175
Ramakrishnan
  • 5,254
  • 9
  • 34
  • 41

1 Answers1

2

The issue is exactly as the described in the error, you are trying to set Request Headers without first calling Open(). You are also missing the Send() method to send the request before a response can be received.

Dim objHttpRequest
Dim gw_menu_request
Dim HTTPMethod

HTTPMethod="POST"
Set objHttpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")    
gw_menu_request = "http://test.com?q=headerexpose/expose_headers/expose_json"
'Open request specifying method and URL to call
objHttpRequest.open HTTPMethod, gw_menu_request, False
'Set any HTTP headers needed before sending.      
objHttpRequest.setRequestHeader "Content-Type", "application/x-www-form-urlencoded" 
objHttpRequest.setRequestHeader "Content-Length", 0
'Send the request
objHttpRequest.Send 
Response.write(objHttpRequest.ResponseXML.Xml)

If Err.Number <> 0 Then 
  Response.Write "Err.Number :" & Err.Number  & "<br/>"
  Response.Write "Err.Source :" & Err.Source & "<br/>"
  Response.Write "Err.Source :" & Err.Description  & "<br/>"
  Response.Write "Err.File :" & Err.File & "<br/>"
End If

You also want ResponseXML.Xml or you will receive a

Microsoft VBScript runtime error: Type mismatch

because you are trying to output the object not the Xml property that contains the XML string representation.

user692942
  • 16,398
  • 7
  • 76
  • 175
  • 1
    @Ramakrishnan Sorry realised I missed out the `Send()` which will also cause the problem due to the `Response` not being available until the `Request` is sent. – user692942 May 12 '16 at 12:16
  • I am getting the same error - "This method cannot be called until the open method has been called" – Ramakrishnan May 12 '16 at 12:16
  • lankymart- How to fix that problem, any clues. – Ramakrishnan May 12 '16 at 12:18
  • @Ramakrishnan Have you looked at my most recent edit in regards to not setting `Send()`?, it's hard to workout which comment you have seen and replied to. – user692942 May 12 '16 at 12:21
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/111753/discussion-between-ramakrishnan-and-lankymart). – Ramakrishnan May 12 '16 at 12:24