Jerry Jacob's Blog

Calling Java Web Service from VB 6.0

Posted in Visual Basic by jerlinworld on September 24, 2009

Given below is the code that I used successfully in my VB Application that call the Java web service.


Public Function ValidateSSN(ByVal Domain As String) As String
    Dim strSoapAction As String
    Dim strUrl As String
    Dim strXml As String

    strUrl = "http://" + Domain + "/JobRepository/services/DetermineJobCode"
    strSoapAction = "http://www.jerryjacob.com/JobRepositoryWS/getOccupations"
    strXml = "<?xml version=""1.0"" encoding=""utf-8""?>" & _
            "<soapenv:Envelope xmlns:soapenv=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:job=""http://www.jerryjacob.com/JobRepositoryWS/"">" & _
            "<soapenv:Header/>" & _
            "<soapenv:Body>" & _
            "<job:getJobs>" & _
            "<getJobsRequest>" & _
            "<jobTitle>Computer Programmers</jobTitle>" & _
            "<maximumResults>1</maximumResults>" & _
            "</getJobsRequest>" & _
            "</job:getJobs>" & _
            "</soapenv:Body>" & _
            "</soapenv:Envelope>"
 
    Debug.Print PostWebservice(strUrl, strSoapAction, strXml)
    ValidateSSN = PostWebservice(strUrl, strSoapAction, strXml)
End Function


Private Function PostWebservice(ByVal AsmxUrl As String, ByVal SoapActionUrl As String, ByVal XmlBody As String) As String
    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")
   
    ' 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

    ' Close object
    Set objXmlHttp = Nothing
   
    ' Extract result
    intPos1 = InStr(strRet, "<score>") + 7
    intPos2 = InStr(intPos1, strRet, "</score>")
    If intPos1 > 7 And intPos2 > 0 Then
        strRet = Mid(strRet, intPos1, intPos2 - intPos1)
    End If
   
    ' Return result
    PostWebservice = strRet
   
Exit Function
Err_PW:
    PostWebservice = "Error: " & Err.Number & " - " & Err.Description

End Function

Advertisements

Comments Off on Calling Java Web Service from VB 6.0