James;
Hi. I might as well attempt to clarify what WebServices are. I may be wrong but I feel like most answers I get seem to miss the concept of consuming web services or what web services are about.
Web services are methods that execute some code on a server via xml data exchange that can also be consumed via SOAP. There are web services out there to perform many services and/or retrieve certain data from other servers. For example, there is a web service that returns any financial information of any given stock code. There is a web service to obtain a city’s information given the zip code. There are web services to manipulate data strings, such as upper(), and lower().
The web services I wish to consume accept a username and passwrd to allow sending a file and respond to that file with another file. The files I wish to upload are known as ANSI X12 format documents. I can supply a few of these documents for testing together with my user name and password for testing the web service. Producing ANSI X12 format documents to be sent to the service is not the question here. That is my expertise. I can also supply or even upload here the companion guide from the WebService creators that explains their webservices with .net sample code on how to consume these services. The webservice is developed by a clearing house named Inmediata- with whom I need to deal with.
Below is some sample code I wrote and tested to consume a freely available webservice to extract US cities information. On my sample code you will see two different methods of consuming this webservice, both function names is FetchZipFromWeb(). The first consumes the webservice using tip() harbour class and then simply trying to parse the response from the server by using harbour's Txlm() class. The second function consumes the same webservices using SOAP. Using SOAP - don't always have a positive result, so for this service I have stayed with TXML. I make extensive use of TXML() harbour class in most of my programs. I simply haven't had any success consuming the webservices in question here. If anyone on this forum has any experience using and consuming webservices, he/she might have better luck than me and I'm willing to shed some money in order to succeed consuming Inmediata's web services.
- Code: Select all Expand view
//-----------------------------------------------------------------------------------------------------
//tipclienthttp() and Turl() classes exist only after ver 1.0 in xharbour.
FUNCTION wfReadURL(cUrl)
LOCAL cPageContent:="Error: " + cUrl + " not found or timed out."
LOCAL oConn//, oUrl := TURL():New( cUrl )
IF UPPER( LEFT( cUrl, 4 ) ) # "HTTP"
cUrl:="http://"+cUrl
ENDIF
TRY
DEBUG "In wfReadURL", cUrl
oConn := TipClientHttp():New( cUrl )
DEBUG "In wfReadURL 2"
oConn:nConnTimeout := 20000
IF oConn:Open()
DEBUG "In wfReadURL 3"
cPageContent := oConn:ReadAll()
DEBUG "In wfReadURL 4"
oConn:Close()
DEBUG "In wfReadURL 5"
ENDIF
DEBUG "In wfReadURL 6"
CATCH
cPageContent:="Error opening " + cUrl
END
RETURN cPageContent
//-----------------------------------------------------------------------------------------------------
FUNCTION fetchZipFromWeb( cCity, cZip )
local cUrl := "http://webservicex.net/uszip.asmx/"
local oXml, oXmlNode, cStr
local aRet := {}
local aRec := {}
if ( cCity == Nil .or. empty( cCity ) ) .and. ;
( cZip == Nil .or. empty( cZip ) )
Return { "No Results Found" }
Endif
cUrl += iif( cZip != Nil .and. !empty( cZip ), ;
"GetInfoByZIP?USZip=" + alltrim( cZip ),;
"GetInfoByCity?USCity="+ alltrim( cCity ) )
oXml := TXmlDocument():New( cStr := wfReadURL( cUrl ) )
oXmlNode := oXml:FindFirst( "CITY" )
While oXmlNode != Nil
aRec := { oXmlNode:cData }
oXmlNode := oXmlNode:NextInTree()
While oXmlNode != Nil .and. oXmlNode:cName != "CITY"
aadd( aRec, oXmlNode:cData )
oXmlNode := oXmlNode:NextInTree()
end
aadd( aRet, aRec )
End
Return aRet
//-----------------------------------------------------------------------------------------------------
/*function FetchZipFromWeb( cCity, cZip )
local aRet := { "No data found" }
local oSoapClient, oResponse, oErr
local lOk := .t., oXml
local ctemp, ctemp2, ctemp3
TRY
oSoapClient := CreateObject( "MSSOAP.SoapClient30" )
oSoapClient:MSSoapInit( "http://www.webservicex.net/uszip.asmx?WSDL" )
// oSoapClient:MSSoapInit( "http://www.dataaccess.com/webservicesserver/textcasing.wso?WSDL" )
// aRet := oSoapClient:InvertStringCase( "lower UPPER" )
CATCH oErr
WriteErrorLog( oErr )
MsgStop( "Soap Client 3.0 not installed. Check Error.log for details." )
lOk := .f.
End
TRY
if lOk .and. !empty( cZip )
oResponse = oSoapClient:GetInfoByZIP( "00950" )
oXml := TXmlDocument():New( oResponse )
xbrowse( oXml )
Elseif lOk .and. !empty( cCity )
oResponse = oSoapClient:GetInfoByCity( cCity )
aRet := { "City:" + AllTrim( cCity ) + " Zip:" + oResponse:GetElementsByTagName( "ZIP" ) }
Endif
CATCH oErr
WriteErrorLog( oErr )
END
DeleteObject( oSoapClient )
DeleteObject( oResponse )
return aRet*/
Reinaldo Crespo-Bazán
reinaldo dot crespo gmail.com