Monday, November 21, 2011

VB Script - Call Web-Service with parameters as inline attachment (Base 64)

VB Script - Call Web-Service with parameters as inline attachment (Base 64)

This code is written to automate web-service testing using QTP and Selenium. Actual code is more complicated, below code will help you understand in construction of web-service for the method mentioned in below screen shot.
Need to modify the SOAP message as per your method parameters and data type.




SOAPMessage = "" & _ 
     "" & Bytes() & "" & _
     "1.xml"

     
     
msgBox SOAPMessage
Set objHTTP = CreateObject ( "MSXML2.XMLHTTP")
Set xmlDOC = CreateObject ( "MSXML.DOMDocument") 
strWebserviceURL = URL '"http://xxx.com/yyyyy/WSFileServer.asmx"
objHTTP.Open "POST", strWebserviceURL, False
'Set the Content-Type is very important 
objHTTP.SetRequestHeader "Content-Type", "text/xml;charset=utf-8"
'objHTTP.SetRequestHeader "SOAPAction", "http://tempuri.org/postPurchaseOrder"
objHTTP.SetRequestHeader "User-Agent", "Jakarta Commons-HttpClient/3.0"
objHTTP.SetRequestHeader "Host", Domain 'xxx.com 
objHTTP.Send (SOAPMessage) 
bOK = xmlDOC.load (objHTTP.responseXML) 
'Look at the state value 
msgBox objHTTP.Status 
msgbox objHTTP.StatusText 
msgbox objHTTP.responseText
'objHTTP.Status = 200, return here to deal with a fragment of xml 
'If necessary, you can replace them to return the xml string of  
xmlStr = xmlDOC.xml 
xmlStr = Replace (xmlStr ,"&lt","<", 1, -1,1) 
xmlStr = Replace (xmlStr ,"&gt",">", 1, -1,1) 
msgbox xmlStr 

' HTTP is a text based protocol, it can't transmit binary content
' so content need to be converted into base 64 format
' Below function perform this job for you

Function Bytes()
 Const fsDoOverwrite     = true  ' Overwrite file with base64 code
 Const fsAsASCII         = false ' Create base64 code file as ASCII file
 Const adTypeBinary      = 1     ' Binary file is encoded

 ' Variables for writing base64 code to file
 Dim objFSO
 Dim objFileOut

 ' Variables for encoding
 Dim objXML
 Dim objDocElem

 ' Variable for reading binary picture
 Dim objStream

 ' Open data stream from picture
 Set objStream = CreateObject("ADODB.Stream")
 objStream.Type = adTypeBinary
 objStream.Open()
 objStream.LoadFromFile("C:\sample\1.xml")

 ' Create XML Document object and root node
 ' that will contain the data
 Set objXML = CreateObject("MSXml2.DOMDocument")
 Set objDocElem = objXML.createElement("Base64Data")
 objDocElem.dataType = "bin.base64"

 ' Set binary value
 objDocElem.nodeTypedValue = objStream.Read()

 ' Get base64 value and write to file
 'objFileOut.Write objDocElem.text
 'msgBox (objDocElem.text)
 Bytes = objDocElem.text

 ' Clean all
 Set objFSO = Nothing
 Set objFileOut = Nothing
 Set objXML = Nothing
 Set objDocElem = Nothing
 Set objStream = Nothing
End function
I am currently busy with other tasks, planning to put detailed explanation of the above code later.

No comments:

Post a Comment