Tuesday, 25 January 2011


Yes, you got it right testing Web services can be done without using Web services add-in. All this is possible with the help of WinHTTP(Microsoft Windows HTTP service), which provides a high-level interface to the HTTP internet protocol.
We can use the com object WinHTTPRequest in our tests in order to invoke any operation to a web service. We will need to post the SOAP request to the web server using methods and properties ofWinHTTPRequest and we can get the corresponding response from the service.
Let us now try this out, open QTP and make sure you have not enabled Web Services Add-in in Add-in manager at startup.To retrieve the COM object of WinHTTP we use CreateObject method
Dim oWinHttp
Set oWinHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
Now you can access the different properties and methods of WinHTTPRequest. I have explained some of them which we will need to use here.
Object.Open Method,URL,Async
Open – opens a connection to an HTTP resource.
Method – specifies the HTTP verb used for the open method, like ‘GET’,’PUT’,’POST’ etc
URL – the name of the resource
Async – indicates whether to open in asynchronous mode.
Object.SetRequestHeader Header,Value
SetRequestHeader – adds, changes, or deletes an HTTP request header
Header Specifies the name of the header to be set like depth,Content type, Content length etc.
Value specified the value of header.
Object.Send Body 
Send – sends an HTTP request to an HTTP server.
Body – is the data to be sent to the server.
We will use one property ‘ResponseText’ here which retrieves the responses entity body as text. Now as we are now aware of these methods, we can use this in our script. Below example script illustrate the accessing of web services using WinHTTP.
Option Explicit
Dim sWebServiceURL, sContentType, sSOAPAction, sSOAPRequest
Dim oWinHttp
Dim sResponse
'Web Service URL
'Web Service Content Type
sContentType ="text/XML"
'Web Service SOAP Action
'Request Body
sSOAPRequest = "<?xml version=""1.0"" encoding=""utf-8""?>" & _
"<soap:Body>" & _
"<CelsiusToFahrenheit xmlns="http://tempuri.org/">" & _
"<Celsius>25</Celsius>" & _
"</CelsiusToFahrenheit>" & _
"</soap:Body>" & _
"</soap:Envelope>"
Set oWinHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
'Open HTTP connection
oWinHttp.Open "POST", sWebServiceURL, False
'Setting request headers
oWinHttp.setRequestHeader "Content-Type", sContentType
oWinHttp.setRequestHeader "SOAPAction", sSOAPAction
'Send SOAP request
oWinHttp.Send  sSOAPRequest
'Get XML Response
sResponse = oWinHttp.ResponseText
' Close object
Set oWinHttp = Nothing
' Extract result
Dim nPos1, nPos2
nPos1 = InStr(sResponse, "Result>") + 7
nPos2 = InStr(sResponse, "</")
If nPos1 > 7 And nPos2 > 0 Then
    sResponse = Mid(sResponse, nPos1, nPos2 - nPos1)
End If
' Return result
msgbox sResponse
This gives you the required expected value

QTP Web Services 
The same we can do using XMLHTTP also, see the example below which illustrate the accessing of web services using XMLHTTP
Option Explicit
Dim sWebServiceURL, sContentType, sSOAPAction, sSOAPRequest
Dim oDom, oXmlHttp
Dim sResponse
sContentType ="text/XML" 'Web Service Content Type
sSOAPAction = "http://tempuri.org/CelsiusToFahrenheit" ' Web Service SOAP Action
'Request Body
sSOAPRequest = "<?xml version="1.0" encoding="utf-8"?>" & _
"<soap:Body>" & _
"<CelsiusToFahrenheit xmlns="http://tempuri.org/">" & _
"<Celsius>25</Celsius>" & _
"</CelsiusToFahrenheit>" & _
"</soap:Body>" & _
"</soap:Envelope>"
'Create objects to DOMDocument and XMLHTTP
Set oDom = CreateObject("MSXML2.DOMDocument")
Set oXmlHttp = CreateObject("MSXML2.XMLHTTP")
'Load XML
oDom.async = False
oDom.loadXML sSOAPRequest
'Open the webservice
 oXmlHttp.open "POST", sWebServiceURL, False
'Create headings
oXmlHttp.setRequestHeader "Content-Type", sContentType
oXmlHttp.setRequestHeader "SOAPAction", sSOAPAction
'Send XML command
oXmlHttp.send oDom.xml
'Get XML Response
sResponse = oXmlHttp.ResponseText
'Close object
Set oXmlHttp = Nothing
'Extract result
Dim nPos1, nPos2
nPos1 = InStr(sResponse, "Result>") + 7
nPos2 = InStr(sResponse, "</")
If nPos1 > 7 And nPos2 > 0 Then
 sResponse = Mid(sResponse, nPos1, nPos2 - nPos1)
End If
'Return result
msgbox sResponse
Option Explicit
Dim sWebServiceURL, sContentType, sSOAPAction, sSOAPRequest
Dim oWinHttp
Dim sResponse
sContentType ="text/XML" 'Web Service Content Type
sSOAPAction = "http://tempuri.org/CelsiusToFahrenheit" ' Web Service SOAP Action
'Request Body
sSOAPRequest = "<?xml version=""1.0"" encoding=""utf-8""?>" & _
"<soap:Body>" & _
"<CelsiusToFahrenheit xmlns=""http://tempuri.org/"">" & _
"<Celsius>25</Celsius>" & _
"</CelsiusToFahrenheit>" & _
"</soap:Body>" & _
"</soap:Envelope>"
Set oWinHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
'Open HTTP connection
oWinHttp.Open "POST", sWebServiceURL, False
'Setting request headers
oWinHttp.setRequestHeader "Content-Type", sContentType
oWinHttp.setRequestHeader "SOAPAction", sSOAPAction
'Send SOAP request
oWinHttp.Send  sSOAPRequest
'Get XML Response
sResponse = oWinHttp.ResponseText
' Close object
Set oWinHttp = Nothing
' Extract result
Dim nPos1, nPos2
nPos1 = InStr(sResponse, "Result>") + 7
nPos2 = InStr(sResponse, "</")
If nPos1 > 7 And nPos2 > 0 Then
sResponse = Mid(sResponse, nPos1, nPos2 - nPos1)
End If
' Return result
msgbox sResponse

1 comment:

  1. Nice post, can share about aws I bookmark your blog because I found very good information on your blog, Thanks for sharing more information.

    ReplyDelete

TrainingHUB. Powered by Blogger.

Total Pageviews

THE BEST QTP TRAINING INSTITUTE IN HYDERABAD

QTP Training in hyderabad

Sql Tutorial

Popular Posts

Our Facebook Page

TrainingHUB

Followers