Invoking a RESTFul WebService using WinHttp - web-services

I am totally new with VB6 and REST architecture. Nevertheless I would like please to know if there is any HelloWorld example for a REST Client which invokes a RESTFul webService using Windows HTTP Services API. Many thanks in advance.

Here's the solution :
Sub SendAsynchMessage()
Dim objHTTP As New WinHttp.WinHttpRequest
Dim doc As New MSXML2.DOMDocument
Dim root As MSXML2.IXMLDOMNode
Dim success As Boolean
Dim str As String
On Error GoTo ErrorHandler
success = doc.Load(App.Path & "\flow.xml")
Set root = doc.selectSingleNode("/root")
str = CStr(root.childNodes.Item(0).xml)
URL = "http://ipAddress:8081/messageAsynch"
objHTTP.Open "POST", url, False
objHTTP.SetRequestHeader "Content-Type", "text/xml; charset=utf-8"
objHTTP.Send (str)
Debug.Print objHTTP.Status
Debug.Print objHTTP.ResponseText
Exit Sub
ErrorHandler:
Dim E As ErrObject: Set E = Err
End Sub
The "flow.xml" file could look like in this case :
<?xml version="1.0" encoding="utf-8" ?>
<root>
<!-- your xml flow to be send via http -->
</root>

Related

How to download an MTOM attachment in VB6

Please, someone can show me an example about how to download an MTOM attachment and save it to disk using VB6?
At the moment I am able to get a response from my ws, using this code:
Dim objDom As Object
Dim objXmlHttp As Object
Dim strRet As String
Dim intPos1 As Integer
Dim intPos2 As Integer
On Error GoTo Err_PW
' Create objects to DOMDocument and XMLHTTP
Set objDom = CreateObject("MSXML2.DOMDocument")
Set objXmlHttp = CreateObject("MSXML2.XMLHTTP")
'Set objXmlHttp = New MSXML2.XMLHTTP40
' Load XML
objDom.async = False
objDom.loadXML XmlBody
' Open the webservice
objXmlHttp.Open "POST", AsmxUrl, False
' Create headings
objXmlHttp.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
objXmlHttp.setRequestHeader "SOAPAction", SoapActionUrl
' Send XML command
objXmlHttp.Send (objDom.xml)
' Get all response text from webservice
strRet = objXmlHttp.responseText
But from this point I don't know how to proceed to grab the MTOM attachment.
This is what return from ws:
--uuid:0b9372cf-63bb-484f-87f3-7bedfaa776fb
Content-Id: <rootpart*0b9372cf-63bb-484f-87f3-7bedfaa776fb#example.jaxws.sun.com>
Content-Type: application/xop+xml;charset=utf-8;type="text/xml"
Content-Transfer-Encoding: binary
<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body><ns2:getContentStreamResponse xmlns:ns2="http://docs.oasis-open.org/ns/cmis/messaging/200908/" xmlns="http://docs.oasis-open.org/ns/cmis/core/200908/">
<ns2:contentStream>
<ns2:length>2572861</ns2:length>
<ns2:mimeType>application/vnd.openxmlformats-officedocument.wordprocessingml.document</ns2:mimeType>
<ns2:filename>SF-MASTER - SDTC.docx</ns2:filename>
<ns2:stream>
<xop:Include xmlns:xop="http://www.w3.org/2004/08/xop/include" href="cid:4b7c8c60-fc9e-4da8-92fc-aa8c2fdfb401#example.jaxws.sun.com"/></ns2:stream>
</ns2:contentStream>
</ns2:getContentStreamResponse>
</S:Body>
</S:Envelope>
--uuid:0b9372cf-63bb-484f-87f3-7bedfaa776fb
Content-Id: <4b7c8c60-fc9e-4da8-92fc-aa8c2fdfb401#example.jaxws.sun.com>
Content-Type: application/vnd.openxmlformats-officedocument.wordprocessingml.document
Content-Transfer-Encoding: binary
PK
31/05/2018 10:04:06: UUID documento: 0b9372cf-63bb-484f-87f3-7bedfaa776fb
31/05/2018 10:04:06: Tag finale: --uuid:0b9372cf-63bb-484f-87f3-7bedfaa776fb--
31/05/2018 10:04:06: lunghezza documento: 2572861
Thank you very much
Use this as a guide and modify your code to save the returned file data as a binary file.
Add to top of function
Const adTypeBinary = 1
Const adSaveCreateOverWrite = 2
Dim varByteArray, ado
Dim strFile as String
After you send the request insert this code
If objXmlHttp.Status <> "200" Then
Exit Function
End If
' Pull this from returned header data
strFile = "SF-MASTER - SDTC.docx"
' get file contents
varByteArray = objXmlHttp.ResponseBody
Set objXmlHttp= Nothing
'Now save the binary file
On Error Resume Next
Set ado = Nothing
Set ado = CreateObject("ADODB.Stream")
ado.Type = adTypeBinary
ado.Open
ado.Write varByteArray
ado.SaveToFile strFile, adSaveCreateOverWrite
ado.Close

Using ServerXMLHTTP to connect to web service

I have a classic asp file that needs to connect to a web service.
I am confused as to how to define the URL for the web service and the Namespace in the SOAPAction.
When I run my code and write a Response.Write for the return value of the method that I am calling in the web service, it either returns the wsdl or the web page for the service
This code displays the web service html as if you are entering the web service .svc url:
Dim strSoapReq
strSoapReq = "<?xml version=""1.0"" encoding=""utf-8"" ?>"
strSoapReq = strSoapReq & "<s:Envelope xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">"
strSoapReq = strSoapReq & "<s:Body>"
strSoapReq = strSoapReq & "<TestMethod xmlns=""http:<serverName:<port>/PagingService/PagingService"">"
strSoapReq = strSoapReq & "</TestMethod>"
strSoapReq = strSoapReq & "</s:Body>"
strSoapReq = strSoapReq & "</s:Envelope>"
'Create server-side component to make requests
Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
URL = "http:<serverName:<port>/PagingService/PagingService.Paging.svc"
httpRequest.Open "GET", URL, False
httpRequest.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
httpRequest.setRequestHeader "SOAPAction", URL & "/TestMethod"
httpRequest.Send(strSoapReq)
Dim strResult
strResult = httpRequest.responseText
Response.Write(vbCrLf & "Result from web service call: " & vbCrLf & strResult)
If I add the ?wsdl to the end of the service url, it shows the WSDL.
How can I call the method in the web service?
UPDATE
I changed my code to the following:
Dim NS, NS_SOAP, NS_SOAPENC, NS_XSI, NS_XSD
NS = "http://<server>/PagingService/"
NS_SOAP = "http://schemas.xmlsoap.org/soap/envelope/"
NS_SOAPENC = "http://schemas.xmlsoap.org/soap/encoding"
NS_XSI = "http://www.w3.org/2001/XMLSchema-instance"
NS_XSD = "http://www.w3.org/2001/XMLSchema"
Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
URL = "http://<server>/PagingService/PagingService.Paging.svc?WSDL"
httpRequest.Open "POST", URL, False
httpRequest.setRequestHeader "Content-Type", "text/xml; charset=utf-8"
httpRequest.setRequestHeader "SOAPAction", "http://<server>/PagingService/TestMethod"
' XML DOM objects.
Dim DOM, Envelope, Body, Operation, Param
' Creates an XML DOM object.
Set DOM = CreateObject("MSXML2.DOMDocument.6.0")
' Creates the main elements.
Set Envelope = DOM.createNode(1, "soap:Envelope", NS_SOAP)
Envelope.setAttribute "xmlns:soapenc", NS_SOAPENC
Envelope.setAttribute "xmlns:xsi", NS_XSI
Envelope.setAttribute "xmlns:xsd", NS_XSD
DOM.appendChild Envelope
Set Body = DOM.createElement("soap:Body")
Envelope.appendChild Body
' Creates an element for the TestMethod function.
Set Operation = DOM.createNode(1, "TestMethod", NS)
Body.appendChild Operation
' Releases the objects.
Set Operation = Nothing
Set Body = Nothing
Set Envelope = Nothing
httpRequest.Send(DOM.xml)
Dim strResult
strResult = httpRequest.status
Response.Write(vbCrLf & "Result from web service call: " & vbCrLf & strResult)
The http.Status result with this code is: 415 - unsupported media
I saw a post with this error and they corrected it by changing the Content-Type. When I tried this:
httpRequest.setRequestHeader "Content-Type", "application/soap+xml; charset=utf-8"
I got the status value of 400 - Bad request.
Finally figured this one out. Below is my code in case some poor minion needs to do this...maybe it will lessen the pain and shorten the time to get it to run.
I connected a WCF web service (file extension, .svc) to a classic .asp file.
I had to add a basicHttpBinding to the web service. It can also be secure (HTTPS).
This is the binding added to the service config. file:
(Note the name of it. defined by address attribute.
<endpoint address="basic" binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IPaging" contract="PagingService.IPaging">
</endpoint>
Added this to the bindings part of the config file:
<basicHttpBinding>
<binding name="BasicHttpBinding_IPaging"></binding>
</basicHttpBinding>
You need to recompile the project so the WSDL has this binding in its definition.
Now the fun part....classic asp :( written in VBScript.
The SOAP envelope gave me the most trouble:
'Namespaces
Dim NS, NS_SOAP, NS_SOAPENC, NS_XSI, NS_XSD, NS_SOAP_ACTION
'NS is the name of YOUR namespace. If you did not define it
'in the service interface it is probably the same as this
NS = "http://tempuri.org/"
'It is for SOAP 1.1 - my version of .asp could only use SOAP 1.1
NS_SOAP = "http://schemas.xmlsoap.org/soap/envelope/"
'Next 3 definitions are standard - just copy
NS_SOAPENC = "http://schemas.xmlsoap.org/soap/encoding"
NS_XSI = "http://www.w3.org/2001/XMLSchema-instance"
NS_XSD = "http://www.w3.org/2001/XMLSchema"
'This should also be in your WSDL. Look up the method you
'want to call; there was an attribute in mine that read 'soap_action'
NS_SOAP_ACTION = "http://tempuri.org/IFileName/<YourMethodName>"
'URL to the WCF service Using basicHttpBinding identified to the name
'you defined in the config file in 'address' attribute
URL = "https://<serverName>:<port>/ServiceFolder/Service.Paging.svc/basic"
'This was the hard part for me. Defining the damn soap message
'XML DOM objects.
Dim Envelope, Body, Operation
Dim ParamUserID, ParamUserName,
'Creates an XML DOM object.
Set objXmlDoc = CreateObject("MSXML2.DOMDocument.6.0")
objXmlDoc.async = false
'Creates the main elements.
Set Envelope = objXmlDoc.createNode(1, "soap:Envelope", NS_SOAP)
Envelope.setAttribute "xmlns:soapenc", NS_SOAPENC
Envelope.setAttribute "xmlns:xsi", NS_XSI
Envelope.setAttribute "xmlns:xsd", NS_XSD
objXmlDoc.appendChild Envelope
Set Body = objXmlDoc.createNode(1, "Body", NS_SOAP)
Envelope.appendChild Body
'Creates an element for the SendPageForGalvanonSystem function.
Set Operation = objXmlDoc.createNode(1, "<MethodName>", NS)
Body.appendChild Operation
'Add all the parameters to the DOM
Set ParamUserID = objXmlDoc.createNode(1, "strUserID", NS)
ParamUserID.text = strUserID
Operation.appendChild ParamUserID
Set ParamUserName = objXmlDoc.createNode(1, "strUserName", NS)
ParamUserName.text = strUserName
Operation.appendChild ParamUserName
Set httpRequest = Server.CreateObject("MSXML2.ServerXMLHTTP.6.0")
httpRequest.Open "POST", URL, False
httpRequest.setRequestHeader "Content-Type", "text/xml"
httpRequest.setRequestHeader "SOAPAction", NS_SOAP_ACTION
httpRequest.send objXmlDoc.xml
'Releases the objects.
Set ParamUserID = Nothing
Set ParamUserName = Nothing
Set Operation = Nothing
Set Body = Nothing
Set Envelope = Nothing

Can I access an external WebServices in Azure Websites (WAWS)

I have a classic ASP website hosted at Azure using Websites.
Now I need this site to access a set of WebServices located externally... I wrote a quick/dirty ASP page to check and it always times out... It seems as it was blocked.
Can anybody point me in the right direction? Please find below my quick test code.
Dim objRequest, objXMLDoc, objXmlNode
Dim strRet, strError, strNome
Dim strName
strName= "?FromCurrency=USD&ToCurrency=BRL"
Set objRequest = Server.createobject("MSXML2.XMLHTTP")
With objRequest
.open "GET", "http://www.webservicex.net/CurrencyConvertor.asmx" & strName, False
.setRequestHeader "Content-Type", "text/xml"
.setRequestHeader "SOAPAction", "http://www.webservicex.net/CurrencyConvertor.asmx"
.send
End With
Set objXMLDoc = Server.createobject("MSXML2.DOMDocument")
objXmlDoc.async = false
Response.ContentType = "text/xml"
Response.Write(objRequest.ResponseText)

ASMX webserive not recieving parameters from classic ASP soap request.

I am attempting to wrap a newer .net ASMX webservice so that it can be used by an older classic asp application. To do this I found some code to send the soap requests. However in my testing it seems that none of my parameters are reaching the server.
The Server Test Code
<WebMethod()> _
Public Function Ping1(str As String)
If str <> "" Then
Return str
Else
Return "False"
End If
End Function
The xml being sent:
<?xml version="1.0" encoding="utf-8"?>
<soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope">
<soap12:Body>
<Ping1 xmlns="http://tempuri.org">
<str>asdf</str>
</Ping1>
</soap12:Body>
</soap12:Envelope>
The page keeps returning "False" but as far as I can tell, this should be the right format to send parameters. Any help would be appreciated.
As demonstrated in this other question you should set proper headers when consuming the service:
oXmlHTTP.setRequestHeader "Content-Type", "application/soap+xml; charset=utf-8"
oXmlHTTP.setRequestHeader "SOAPAction", "http://ourNameSpace/Ping1"
Note that you can't use tempuri.org namespace, you have to also set your own namespace.

VB6 application calling Rest Service

I have to call a REST service from VB6. I followed this post
Get/post to RESTful web service but doesnt able to achieve much.
What I have done so far is :
Dim sUrl As String
Dim response As String
Dim xmlhttp
sUrl = "myserviceurl"
xmlhttp = CreateObject("MSXML2.xmlhttp")
xmlhttp.open "GET", sUrl, False
xmlhttp.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
Set xmlhttp = Nothing
Have added MSXML6 to my project references.
I get this error :
Object doesnt support this Method..
Try below code
Dim xmlhttp As MSXML2.XMLHTTP
Set xmlhttp = New MSXML2.XMLHTTP