[Gambas-user] gambas3 and web services [bigger, better, faster, more]
Caveat
Gambas at ...1950...
Tue Jun 21 16:56:42 CEST 2011
Hi Massimo,
I just extended the example program I worked up to include all 4 main
ways of calling a Web Service. These are: Http GET, Http POST, SOAP
1.1, and SOAP 1.2.
One problem I ran into is that Gambas3 seems a little too fussy about
the valid content types and will give a runtime error on ones it doesn't
like but it seems like they are perfectly valid. I found the trick was
to set the content-type as first param on the call to HttpClient.Post()
to empty string and 'manually' send a Content-Type header.
Be aware that the public Web Service used here can be a little flaky,
and does time out now and then (guess it depends how many people are
hitting their server at once...).
You can find the code on pastebin, nicely formatted, at
http://pastebin.com/2gyxGzAP and here's the code in plain text style:
************************************************************************************************
Public Sub Test_Click()
Dim http As HttpClient
http = New HttpClient As "http"
doHttpGet(http)
showResult("Http GET", http)
http = New HttpClient As "http"
doHttpPost(http)
showResult("Http POST", http)
http = New HttpClient As "http"
doSOAP11(http)
showResult("SOAP 1.1", http)
http = New HttpClient As "http"
doSOAP12(http)
showResult("SOAP 1.2", http)
End
Public Sub doHttpGet(http As HttpClient)
http.URL =
"http://www.webservicex.net/ConvertTemperature.asmx/ConvertTemp?Temperature=100&FromUnit=degreeCelsius&ToUnit=degreeFahrenheit"
http.Async = False
http.Timeout = 60
http.Get()
End
Public Sub doHttpPost(http As HttpClient)
Dim data As String
http.URL =
"http://www.webservicex.net/ConvertTemperature.asmx/ConvertTemp"
http.Async = False
http.Timeout = 60
data =
"Temperature=100&FromUnit=degreeCelsius&ToUnit=degreeFahrenheit"
Print "Http POST data: " & data
http.Post("application/x-www-form-urlencoded", data)
End
Public Sub doSoap11(http As HttpClient)
Dim data, aHeader As String
Dim headers As String[]
http.URL = "http://www.webservicex.net/ConvertTemperature.asmx"
http.Async = False
http.Timeout = 60
data = "<?xml version=" & Chr$(34) & "1.0" & Chr$(34) & " encoding=" &
Chr$(34) & "utf-8" & Chr$(34) & "?>" & gb.newline &
"<soap:Envelope xmlns:xsi=" & Chr$(34) &
"http://www.w3.org/2001/XMLSchema-instance" & Chr$(34) & " xmlns:xsd=" &
Chr$(34) & "http://www.w3.org/2001/XMLSchema" & Chr$(34) & "
xmlns:soap=" & Chr$(34) & "http://schemas.xmlsoap.org/soap/envelope/" &
Chr$(34) & ">" & gb.newline &
" <soap:Body>" & gb.newline &
" <ConvertTemp xmlns=" & Chr$(34) & "http://www.webserviceX.NET/" &
Chr$(34) & ">" & gb.newline &
" <Temperature>100</Temperature>" & gb.newline &
" <FromUnit>degreeCelsius</FromUnit>" & gb.newline &
" <ToUnit>degreeFahrenheit</ToUnit>" & gb.newline &
" </ConvertTemp>" & gb.newline &
" </soap:Body>" & gb.newline &
"</soap:Envelope>"
Print "Soap 1.1 data: " & data
headers = ["Content-Type: text/xml; charset=utf-8", "Content-Length: "
& Len(data), "SOAPAction: " & Chr$(34) &
"http://www.webserviceX.NET/ConvertTemp" & Chr$(34)]
For Each aHeader In headers
Print "Sending SOAP 1.1 header: " & aHeader
Next
http.Post("", data, headers)
End
Public Sub doSoap12(http As HttpClient)
Dim data, aHeader As String
Dim headers As String[]
http.URL = "http://www.webservicex.net/ConvertTemperature.asmx"
http.Async = False
http.Timeout = 60
data = "<?xml version=" & Chr$(34) & "1.0" & Chr$(34) & " encoding=" &
Chr$(34) & "utf-8" & Chr$(34) & "?>" & gb.newline &
"<soap12:Envelope xmlns:xsi=" & Chr$(34) &
"http://www.w3.org/2001/XMLSchema-instance" & Chr$(34) & " xmlns:xsd=" &
Chr$(34) & "http://www.w3.org/2001/XMLSchema" & Chr$(34) & "
xmlns:soap12=" & Chr$(34) & "http://www.w3.org/2003/05/soap-envelope" &
Chr$(34) & ">" & gb.newline &
" <soap12:Body>" & gb.newline &
" <ConvertTemp xmlns=" & Chr$(34) & "http://www.webserviceX.NET/" &
Chr$(34) & ">" & gb.newline &
" <Temperature>100</Temperature>" & gb.newline &
" <FromUnit>degreeCelsius</FromUnit>" & gb.newline &
" <ToUnit>degreeFahrenheit</ToUnit>" & gb.newline &
" </ConvertTemp>" & gb.newline &
" </soap12:Body>" & gb.newline &
"</soap12:Envelope>"
Print "Soap 1.2 data: " & data
headers = ["Content-Type: application/soap+xml; charset=utf-8",
"Content-Length: " & Len(data)]
For Each aHeader In headers
Print "Sending SOAP 1.2 header: " & aHeader
Next
http.Post("", data, headers)
End
Public Sub showResult(typeOfCall As String, http As HttpClient)
Dim buffer, aHeader As String
Print "*** Result for " & typeOfCall & " ***"
If http.Status < 0 Then
Print "ERROR"
Else
' Success - read data
If Lof(http) Then Read #http, buffer, Lof(http)
Print buffer
For Each aHeader In http.Headers
Print "Got header: " & aHeader
Next
End If
Print "*** End " & typeOfCall & " ***"
End
************************************************************************************************
Results are as follows:
*** Result for Http GET ***
<?xml version="1.0" encoding="utf-8"?>
<double xmlns="http://www.webserviceX.NET/">212</double>
Got header: HTTP/1.1 200 OK
Got header: Cache-Control: private, max-age=0
Got header: Content-Length: 96
Got header: Content-Type: text/xml; charset=utf-8
Got header: Server: Microsoft-IIS/7.0
Got header: X-AspNet-Version: 4.0.30319
Got header: X-Powered-By: ASP.NET
Got header: Date: Tue, 21 Jun 2011 14:44:45 GMT
*** End Http GET ***
Http POST data:
Temperature=100&FromUnit=degreeCelsius&ToUnit=degreeFahrenheit
*** Result for Http POST ***
<?xml version="1.0" encoding="utf-8"?>
<double xmlns="http://www.webserviceX.NET/">212</double>
Got header: HTTP/1.1 200 OK
Got header: Cache-Control: private, max-age=0
Got header: Content-Length: 96
Got header: Content-Type: text/xml; charset=utf-8
Got header: Server: Microsoft-IIS/7.0
Got header: X-AspNet-Version: 4.0.30319
Got header: X-Powered-By: ASP.NET
Got header: Date: Tue, 21 Jun 2011 14:45:06 GMT
*** End Http POST ***
Soap 1.1 data: <?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ConvertTemp xmlns="http://www.webserviceX.NET/">
<Temperature>100</Temperature>
<FromUnit>degreeCelsius</FromUnit>
<ToUnit>degreeFahrenheit</ToUnit>
</ConvertTemp>
</soap:Body>
</soap:Envelope>
Sending SOAP 1.1 header: Content-Type: text/xml; charset=utf-8
Sending SOAP 1.1 header: Content-Length: 445
Sending SOAP 1.1 header: SOAPAction:
"http://www.webserviceX.NET/ConvertTemp"
*** Result for SOAP 1.1 ***
<?xml version="1.0" encoding="utf-8"?><soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><ConvertTempResponse xmlns="http://www.webserviceX.NET/"><ConvertTempResult>212</ConvertTempResult></ConvertTempResponse></soap:Body></soap:Envelope>
Got header: HTTP/1.1 200 OK
Got header: Cache-Control: private, max-age=0
Got header: Content-Length: 367
Got header: Content-Type: text/xml; charset=utf-8
Got header: Server: Microsoft-IIS/7.0
Got header: X-AspNet-Version: 4.0.30319
Got header: X-Powered-By: ASP.NET
Got header: Date: Tue, 21 Jun 2011 14:45:19 GMT
*** End SOAP 1.1 ***
Soap 1.2 data: <?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>
<ConvertTemp xmlns="http://www.webserviceX.NET/">
<Temperature>100</Temperature>
<FromUnit>degreeCelsius</FromUnit>
<ToUnit>degreeFahrenheit</ToUnit>
</ConvertTemp>
</soap12:Body>
</soap12:Envelope>
Sending SOAP 1.2 header: Content-Type: application/soap+xml;
charset=utf-8
Sending SOAP 1.2 header: Content-Length: 453
*** Result for SOAP 1.2 ***
<?xml version="1.0" encoding="utf-8"?><soap:Envelope
xmlns:soap="http://www.w3.org/2003/05/soap-envelope"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"><soap:Body><ConvertTempResponse xmlns="http://www.webserviceX.NET/"><ConvertTempResult>212</ConvertTempResult></ConvertTempResponse></soap:Body></soap:Envelope>
Got header: HTTP/1.1 200 OK
Got header: Cache-Control: private, max-age=0
Got header: Content-Length: 365
Got header: Content-Type: application/soap+xml; charset=utf-8
Got header: Server: Microsoft-IIS/7.0
Got header: X-AspNet-Version: 4.0.30319
Got header: X-Powered-By: ASP.NET
Got header: Date: Tue, 21 Jun 2011 14:45:37 GMT
*** End SOAP 1.2 ***
Hope this helps you to get your Web Service client up and running using
whichever protocol suits you best.
Regards,
Caveat
O> > On Mon, 2011-06-20 at 09:37 +0200, Almanova Sistemi wrote:
> > > > Hallo to everyone,
> > > >
> > > > I am Massimo and I try to write a client software (in gambas 3) that use
> > > > a web services.
> > > > In other word the software send data with saop protocol an receive data
> > > > with the same protocol.
> > > >
> > > > The information about the availebe services are described in a WSDL file.
> > > >
> > > > Now, hor Can I do this with gambas?
> > > >
> > > > Best Regards,
> > > > Massimo.
> > > > ------------------------------------------------------------------------------
> > > > EditLive Enterprise is the world's most technically advanced content
> > > > authoring tool. Experience the power of Track Changes, Inline Image
> > > > Editing and ensure content is compliant with Accessibility Checking.
> > > > http://p.sf.net/sfu/ephox-dev2dev
> > > > _______________________________________________
> > > > Gambas-user mailing list
> > > > Gambas-user at ...1107...
> > > > https://lists.sourceforge.net/lists/listinfo/gambas-user
> > >
> >
> > ------------------------------------------------------------------------------
> > EditLive Enterprise is the world's most technically advanced content
> > authoring tool. Experience the power of Track Changes, Inline Image
> > Editing and ensure content is compliant with Accessibility Checking.
> > http://p.sf.net/sfu/ephox-dev2dev
> > _______________________________________________ Gambas-user mailing list Gambas-user at lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/gambas-user
>
>
>
> ------------------------------------------------------------------------------
> EditLive Enterprise is the world's most technically advanced content
> authoring tool. Experience the power of Track Changes, Inline Image
> Editing and ensure content is compliant with Accessibility Checking.
> http://p.sf.net/sfu/ephox-dev2dev
> _______________________________________________
> Gambas-user mailing list
> Gambas-user at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/gambas-user
More information about the User
mailing list