[Gambas-user] requests to gambas cgi scripts issues

Caveat Gambas at ...1950...
Tue Aug 5 23:59:25 CEST 2014


Here's a mail I wrote back in 2011 on calling web services from out of 
Gambas, perhaps it helps?

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



On 05/08/14 21:50, PICCORO McKAY Lenz wrote:
> i try to comunicate a desktop gambas app with a cgi web gabmas app, but
> seems i do not know how to make the request from the desktop app.
> i made that code:
>
> =================
> Dim sCad As String
> POA = New HttpClient As "POA"
>
>      sCad = "<html>"
>      sCad = sCad & "<form action=\"http://localhost/test1.webpage\"
> method=post enctype=\"application/x-www-form-urlencoded\" >"
>      sCad = sCad & "<input type=\"text\" name=\"p1usuario\" value=\"name1\"
> />"
>      sCad = sCad & "<input type=\"text\" name=\"p1clave\" value=\"123456\"
> />"
>      sCad = sCad & "<input type=\"submit\" value=\"Submit\" />"
>      sCad = sCad & "</form>"
>      sCad = sCad & "</html>"
>      POA.URL = "http://" & hostcentral &
> "/serialservicio/SSpeticiones1.webpage"
>      POA.Post("text/html", sCad)
> =================
>
> but do not work,
>
> with a simpel HTML form works perfectly, if i doing in wrong way , please
> telme how could i send data on request from a desktop gambas apllication to
> a web gambas aplication (some thing like webservice, jeje)
>
> the cgi script runs perfectly:
>
> =================
> #!/usr/bin/env gbw3
> <%
> USE "gb.vb"
>
> Dim printout As String
> Dim llamada As String
> Dim p1usuario As String
> Dim p1clave As String
>
>      p1usuario = Request.post["p1usuario"]
>      If (String.Comp(p1usuario, "") = 0) Then
>              printout = "-1"
>          Else
>              p1clave = Request.post["p1clave"]
>              printout = "exito: " & p1usuario & " " & p1clave
>          Endif
>      Endif
> Endif
> %>
>
> <%= printout %>
> =================
>
> i tested and works with a html form:
>
> ===================
> <html>
> <form action="http://localhost/test1.webpage" method=post
> enctype="application/x-www-form-urlencoded" >
> <input type="text" name="p1usuario" value="as" />
> <input type="text" name="p1clave" value="asd" />
> <input type="submit" value="pepe">
> </form>
> </html>
> ===================
>
>
> Lenz McKAY Gerardo (PICCORO)
> http://qgqlochekone.blogspot.com
> ------------------------------------------------------------------------------
> Infragistics Professional
> Build stunning WinForms apps today!
> Reboot your WinForms applications with our WinForms controls.
> Build a bridge from your legacy apps to the future.
> http://pubads.g.doubleclick.net/gampad/clk?id=153845071&iu=/4140/ostg.clktrk
> _______________________________________________
> 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