Relevant Codes

A Test Development Resource for HP QuickTest Professional.

Retrieve Server Response using XMLHTTP

by Anshoo Arora on August 10, 2009

I recently received the following question in my mailbox yesterday: Is it possible to retrieve the text of a page without physically opening it?

Yes, it’s possible to retrieve the document text without physically opening the page. It can be done though an XMLHTTP Request. XMLHTTP is short for Extensible Markup Language HyperText Transfer Protocol. “The XMLHttpRequest object implements an interface exposed by a scripting engine that allows scripts to perform HTTP client functionality, such as submitting form data or loading data from a server.”1

In the snippet below, I’ve created a simple mechanism to retrieve server response from this blog. The ‘OPEN’ method is used to assign a method (GET, POST, HEAD) of the pending request. The ‘SEND’ method sends the request (as detailed in the Open method) to the server.

Dim oXMLHTTP
Set oXMLHTTP = CreateObject( "Microsoft.XMLHttp" )
 
With oXMLHTTP
    .open "GET", "http://www.relevantcodes.blogspot.com", False
    .send
 
    Reporter.ReportEvent micInfo, "Status", .statusText
    Reporter.ReportEvent micInfo, "Response", .responseText
End with
 
Set oXMLHTTP = Nothing
XMLHTTP Request Status

XMLHTTP Request Status

XMLHTTP Request Response

XMLHTTP Request Response

References

  1. The XMLHTTPRequest Object
  2. The XMLHTTPRequest Object

If you have any questions, please ask them in the comments section. If your query is confidential, please use the Contact Form to send me an e-mail instead.

{ 3 comments… read them below or add one }

1 Vishal CH September 7, 2009 at 11:49 pm

Hi Anshoo,

When i run the above code snippet i m getting an error ” Acess is denied ” at ‘.Send’ line cld u plz re look into it or what can i do for that?

Thanks in advance,

Vishal

Reply

2 Anshoo Arora September 8, 2009 at 4:58 pm

Hi Vishal,

Actually, there can be multiple reasons for this error. Could you please share with me the code that you executed and the browser(s) installed on your system?

In the meantime, I would recommend you to read the following discussions:

1. XMLHTTPRequest – Permission Denied Error
2. How do I read the contents of a remote web page?
3. Problem using MS XMLHTTPRequest via https
4. StackOverflow.com: VBScript, MSXML and Namespaces

Regards,

Anshoo

Reply

3 Bibek Khatiwara December 23, 2009 at 5:05 am

Ya Vishal..it’s working fine..

Dim oXMLHTTP
Set oXMLHTTP = CreateObject( “Microsoft.XMLHttp” )

With oXMLHTTP
.open “GET”, “http://www.google.co.in/”, False
.send

Stats=.statusText

msgbox Stats
End with

Set oXMLHTTP = Nothing

Reply

Leave a Comment

Previous post:

Next post: