WebService: lettura di dati da un servizio WebApi REST

' In questo esempio viene effettuata la chiamata ad un servizio WebApi REST (webMethod) che restituisce informazioni in formato Json

Dim url As String = "http://192.168.0.244:8999/api/UDC/DecodificaBarcode?barcode=" + escape(form.findcontrol("BARCODE").Value)

Dim webRequest__1 As Net.HttpWebRequest = DirectCast(Net.WebRequest.Create(url), Net.HttpWebRequest)
webRequest__1.Method = "GET"
webRequest__1.ContentType = "application/json"
webRequest__1.Accept = "application/json"

' Recupero della risposta
Dim ok as boolean = true

try
	Using resp As Net.HttpWebResponse = DirectCast(webRequest__1.GetResponse(), Net.HttpWebResponse)
		dim responseStream as System.io.Stream = resp.GetResponseStream()
		dim sr as System.io.StreamReader = new System.io.StreamReader(responseStream)
		Dim json As String = sr.ReadToEnd()  ' This reads the JSon with the result
		
		Dim obj As Newtonsoft.Json.linq.JObject = Newtonsoft.Json.linq.JObject.Parse(json)
		
		if trim(ctype(obj("CodiceArticolo"), Newtonsoft.Json.Linq.JValue).value) = vbnullstring then
			form.findcontrol("BARCODE").Value = ""
			
			form.findcontrol("ARTICOLO").Value = ""
			form.findcontrol("INDMOD").Value = ""
			form.findcontrol("QTAART").Value = ""
			form.findcontrol("LOTTO").Value = ""
			
			form.findcontrol("TITOLO").Value = ""		
		else
			form.findcontrol("ARTICOLO").Value = ctype(obj("CodiceArticolo"), Newtonsoft.Json.Linq.JValue).value
			form.findcontrol("INDMOD").Value = ctype(obj("IndiceModifica"), Newtonsoft.Json.Linq.JValue).value
			form.findcontrol("QTAART").Value = ctype(obj("Quantita"), Newtonsoft.Json.Linq.JValue).value
			form.findcontrol("LOTTO").Value = ctype(obj("Lotto"), Newtonsoft.Json.Linq.JValue).value
			
			form.findcontrol("TITOLO").Value = "Art. : " + form.findcontrol("ARTICOLO").Value + " - Ind. Mod. : " + form.findcontrol("INDMOD").Value + " - Lotto : " + form.findcontrol("LOTTO").Value
		end if
	End Using
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
		
		form.findcontrol("BARCODE").Value = ""
		
		form.findcontrol("ARTICOLO").Value = ""
		form.findcontrol("INDMOD").Value = ""
		form.findcontrol("QTAART").Value = ""
		form.findcontrol("LOTTO").Value = ""
		
		form.findcontrol("TITOLO").Value = ""		
	End Using 
end try