As part of our functional tests, there might be a need to invoke a web service directly. We can do that by using the Web Service Add-in of the QTP, or by a simple VbScript code [which would be much faster] as shown below.
Sample Code:
'''----------------------------------------------------------------------------------------------------------------------
'The URL of the site or of a webservice which we need to access
sURL = "http://intranetportal.anycompany.com/"
'Create the WinHTTPRequest COM Object
Set oWinHttpReq = CreateObject("WinHttp.WinHttpRequest.5.1")
'Create an HTTP request
oWinHttpReq.Open "POST", sURL, False
'Send the HTTP request
oWinHttpReq.Send()
'Wait 4 sec for the Response to come
oWinHttpReq.WaitForResponse(4)
'Retrieve the Status
MsgBox oWinHttpReq.StatusText
'Retrieve the response text
MsgBox oWinHttpReq.ResponseText
'''----------------------------------------------------------------------------------------------------------------------
''These can be added later if needed
''sContentType = "text/XML"
''oWinHttpReq.setRequestHeader "Content-Type", sContentType
''oWinHttpReq.setRequestHeader "Accept", sContentType
''oWinHttpReq.setRequestHeader "Connection", "keep-alive"
''oWinHttpReq.SetTimeouts 5,5,5,5
''oWinHttpReq.Option WinHttpRequestOption_URL
'''----------------------------------------------------------------------------------------------------------------------
Notes:
- HTTP Verb: The HTTP verb (or HTTP method) is an instruction sent in a request message that notifies an HTTP server of the action to perform on the specified resource. For example, "GET" specifies that a resource is being retrieved from the server. Common verbs include "GET", "POST", and "HEAD". For more information and a complete list of standard HTTP verbs, see the HTTP/1.1 specification.
- The Open method does not establish a connection to the resource as the name might imply. Rather, it initializes the internal data structures that maintain information about the session, connection, and request. The HTTP verb "GET" obtains data from the URL. False implies that the transaction occurs synchronously
- The Send method assembles the request headers and sends the request. When called in synchronous mode, the Send method also waits for a response before allowing the application to continue. Sometimes the operations may get timed out but its not because of some error in the code but because it may be blocked by the company firewall. To overcome this, try using the URL of an Internal Site
No comments:
Post a Comment