' In questo esempio viene effettuata la chiamata ad un servizio WebApi REST (webMethod) per inviare dati in formato Json
Dim url As String = "http://192.168.0.244:8999/api/UDC/VersaDaProduzione"
Dim webRequest__1 As Net.HttpWebRequest = DirectCast(Net.WebRequest.Create(url), Net.HttpWebRequest)
webRequest__1.Method = "POST"
webRequest__1.ContentType = "application/json"
webRequest__1.Accept = "application/json"
' Creazione del JSON di invio dei dati
dim json as string
json = "{"
json += """BrcArt"": """ +form.findcontrol("BARCODE").Value + ""","
json += """UbcOrig"": """ +form.findcontrol("UBCORIG").Value + ""","
json += """UbcDest"": """ +form.findcontrol("UBCDEST").Value + ""","
json += """UdcDest"": """ +form.findcontrol("UDCDEST").Value + ""","
json += """Qta"": " + cstr(form.findcontrol("QTA").Value) + ","
json += """IdDms"": " + cstr(form.findcontrol("IDDMS").Value) + ","
json += """Altezza"": " + cstr(form.findcontrol("ALTEZZA").Value)
json += "}"
' ATTENZIONE: in alcuni casi viene restituito un errore del tipo "Richiesta annullata per chiusura del flusso prima dell'invio di tutti i dati". In quel caso, bisogna togliere l'assegnazione seguente
webRequest__1.ContentLength = json.ToString().Length
Using requestWriter2 As New IO.StreamWriter(webRequest__1.GetRequestStream())
requestWriter2.Write(json)
End Using
'Recupero della risposta
Dim ok as boolean = true
dim resp As Net.HttpWebResponse
try
resp = DirectCast(webRequest__1.GetResponse(), Net.HttpWebResponse)
if resp.StatusCode = 200 then
dim responseStream as System.io.Stream = resp.GetResponseStream()
dim sr as System.io.StreamReader = new System.IO.StreamReader(responseStream)
Dim jsonr As String = sr.ReadToEnd() ' This reads the JSon with the result
form.Alert(jsonr)
form.findcontrol("GIA_SPED").Value = true
form.Save()
end if
catch e as net.webexception
Using response As net.WebResponse = e.Response
Dim httpResponse As net.HttpWebResponse = CType(response, Net.HttpWebResponse)
dim errCode as string
errCode = ltrim(str(httpResponse.StatusCode))
Using data As System.IO.Stream = response.GetResponseStream()
Using reader = New System.IO.StreamReader(data)
Dim text As String = reader.ReadToEnd()
form.Alert("Error Code: " + errCode + vbnewline + text)
End Using
End Using
End Using
end try