How to call a Kapow Web Service from VB.NET
Summary
Question:
How to call a Kapow RESTFul Web Service from a VB.NET code
Answer:
- Open Kapow Designer.
- Deploy a robot that returns a complex variable.
- Open the Kapow Management Console.
- Go to Repository > Robots and locate the robot.
- Click on the REST icon for that robot.
- Take note of the REST URL.
- Select the desired format for the request and copy the request string.
- Open Microsoft Visual Basic.
- Create a Web Form page with a button and a textbox
- Assign the code to call the service and to store the result in the textbox.
Here is a sample of this code:
Protected Sub Btn_Sync_Click(sender As Object, e As EventArgs) Handles
Btn_Sync.Click
REST_URL =
"http://ServerName:8080/ManagementCon...EST_Demo.robot"
'Enter your robot's REST URL
REST_Request = 'this is an example where the pc name will be returned
"{
""parameters"" : [ {
""variableName"" : ""restDemoINPUT"",
""attribute"" : [ {
""type"" : ""text"",
""name"" : ""INPUT_Info"",
""value"" : """ + Environment.MachineName + """
} ]
} ]
}"
REST_Response = ""
'Invoke RESTFUL POST KAPOW Synchronous Dim JsonSring = REST_Request
Dim myUri As New Uri(REST_URL)
Dim data = Encoding.Default.GetBytes(JsonSring)
REST_Response = Send_REST_Request(myUri, data, "application/json", "POST")
'Send_REST_Request is documented below
'Extract Result from XML
Dim Xml As XmlDocument
Xml = New XmlDocument
Xml.LoadXml(REST_Response)
Dim nodeList As XmlNodeList = Xml.GetElementsByTagName("attribute")
For Each node As XmlElement In nodeList
If node.Attributes(1).InnerText = "INPUT_Info" Then TextBox.Text =
node.InnerText 'INPUT_Info is the variable returned by the web service.
Next
End Sub
Private Function Send_REST_Request(uri As Uri, jsonDataBytes As Byte(), contentType
As String, method As String) As String
Dim req As WebRequest = WebRequest.Create(uri) req.ContentType = contentType
req.Method = method
req.ContentLength = jsonDataBytes.Length
Dim stream = req.GetRequestStream()
stream.Write(jsonDataBytes, 0, jsonDataBytes.Length) stream.Close()
Dim response = req.GetResponse().GetResponseStream() Dim reader As New StreamReader(response)
Dim res = reader.ReadToEnd()
reader.Close()
response.Close()
Return res
End Function
Note: if you are using Kapow Enterprise under Tomcat, you have to configure the service authentication to prevent having an error (401) Unauthorized:
Open the Kapow Management Console.
Go to Admin > Projects.
Edit the project associated with your robot.
Go to the Services tab
Uncheck the "Authenticate REST/SOPA requests" checkbox. Note: when authentication is disabled, anyone with access to the application can invoke these services, so be careful when selecting which robots to put into this project.
Optionally, you may fill in Access-Control-Allow-Origin to control which domains may call the service; you may use * to allow all.
Keywords: Kapow RESTful Web Service Synchronous Asynchronous, Kapow, Robotic Process Automation