{"id":25381,"date":"2018-12-10T08:30:36","date_gmt":"2018-12-10T07:30:36","guid":{"rendered":"http:\/\/help.qualiware.it\/qw-help\/?p=25381"},"modified":"2018-12-17T12:15:56","modified_gmt":"2018-12-17T11:15:56","slug":"webservice-lettura-di-dati-da-un-servizio-webapi-rest","status":"publish","type":"post","link":"https:\/\/help.qualiware.it\/qw-help\/webservice-lettura-di-dati-da-un-servizio-webapi-rest\/","title":{"rendered":"WebService: lettura di dati da un servizio WebApi REST"},"content":{"rendered":"<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\">' In questo esempio viene effettuata la chiamata ad un servizio WebApi REST (webMethod) che restituisce informazioni in formato Json\r\n\r\nDim url As String = \"http:\/\/192.168.0.244:8999\/api\/UDC\/DecodificaBarcode?barcode=\" + escape(form.findcontrol(\"BARCODE\").Value)\r\n\r\nDim webRequest__1 As Net.HttpWebRequest = DirectCast(Net.WebRequest.Create(url), Net.HttpWebRequest)\r\nwebRequest__1.Method = \"GET\"\r\nwebRequest__1.ContentType = \"application\/json\"\r\nwebRequest__1.Accept&nbsp;=&nbsp;\"application\/json\"\r\n\r\n' Recupero della risposta\r\nDim ok as boolean = true\r\n\r\ntry\r\n\tUsing resp As Net.HttpWebResponse = DirectCast(webRequest__1.GetResponse(), Net.HttpWebResponse)\r\n\t\tdim responseStream as System.io.Stream = resp.GetResponseStream()\r\n\t\tdim sr as System.io.StreamReader = new System.io.StreamReader(responseStream)\r\n\t\tDim json As String = sr.ReadToEnd()  ' This reads the JSon with the result\r\n\t\t\r\n\t\tDim obj As Newtonsoft.Json.linq.JObject = Newtonsoft.Json.linq.JObject.Parse(json)\r\n\t\t\r\n\t\tif trim(ctype(obj(\"CodiceArticolo\"), Newtonsoft.Json.Linq.JValue).value) = vbnullstring then\r\n\t\t\tform.findcontrol(\"BARCODE\").Value = \"\"\r\n\t\t\t\r\n\t\t\tform.findcontrol(\"ARTICOLO\").Value = \"\"\r\n\t\t\tform.findcontrol(\"INDMOD\").Value = \"\"\r\n\t\t\tform.findcontrol(\"QTAART\").Value = \"\"\r\n\t\t\tform.findcontrol(\"LOTTO\").Value = \"\"\r\n\t\t\t\r\n\t\t\tform.findcontrol(\"TITOLO\").Value = \"\"\t\t\r\n\t\telse\r\n\t\t\tform.findcontrol(\"ARTICOLO\").Value = ctype(obj(\"CodiceArticolo\"), Newtonsoft.Json.Linq.JValue).value\r\n\t\t\tform.findcontrol(\"INDMOD\").Value = ctype(obj(\"IndiceModifica\"), Newtonsoft.Json.Linq.JValue).value\r\n\t\t\tform.findcontrol(\"QTAART\").Value = ctype(obj(\"Quantita\"), Newtonsoft.Json.Linq.JValue).value\r\n\t\t\tform.findcontrol(\"LOTTO\").Value = ctype(obj(\"Lotto\"), Newtonsoft.Json.Linq.JValue).value\r\n\t\t\t\r\n\t\t\tform.findcontrol(\"TITOLO\").Value = \"Art. : \" + form.findcontrol(\"ARTICOLO\").Value + \" - Ind. Mod. : \" + form.findcontrol(\"INDMOD\").Value + \" - Lotto : \" + form.findcontrol(\"LOTTO\").Value\r\n\t\tend if\r\n\tEnd Using\r\ncatch e as net.webexception\r\n\tUsing response As net.WebResponse = e.Response\r\n\t\tDim httpResponse As net.HttpWebResponse = CType(response, Net.HttpWebResponse)\r\n\t\tdim errCode as string\r\n\t\t\r\n\t\terrCode = ltrim(str(httpResponse.StatusCode))\r\n\t\t\r\n\t\tUsing data As System.IO.Stream = response.GetResponseStream()\r\n\t\t\tUsing reader = New System.IO.StreamReader(data)\r\n\t\t\t\tDim text As String = reader.ReadToEnd()\r\n\t\t\t\tform.Alert(\"Error Code: \" + errCode + vbnewline + text)\r\n\t\t\tEnd Using\r\n\t\tEnd Using\r\n\t\t\r\n\t\tform.findcontrol(\"BARCODE\").Value = \"\"\r\n\t\t\r\n\t\tform.findcontrol(\"ARTICOLO\").Value = \"\"\r\n\t\tform.findcontrol(\"INDMOD\").Value = \"\"\r\n\t\tform.findcontrol(\"QTAART\").Value = \"\"\r\n\t\tform.findcontrol(\"LOTTO\").Value = \"\"\r\n\t\t\r\n\t\tform.findcontrol(\"TITOLO\").Value = \"\"\t\t\r\n\tEnd Using \r\nend try\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8216; In questo esempio viene effettuata la chiamata ad un servizio WebApi REST (webMethod) che restituisce informazioni in formato Json Dim url As String = &#8220;http:\/\/192.168.0.244:8999\/api\/UDC\/DecodificaBarcode?barcode=&#8221; + escape(form.findcontrol(&#8220;BARCODE&#8221;).Value) Dim webRequest__1 As Net.HttpWebRequest = DirectCast(Net.WebRequest.Create(url), Net.HttpWebRequest) webRequest__1.Method = &#8220;GET&#8221; webRequest__1.ContentType = &#8220;application\/json&#8221; webRequest__1.Accept&nbsp;=&nbsp;&#8220;application\/json&#8221; &#8216; Recupero della risposta Dim ok as boolean = true try Using resp&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"ngg_post_thumbnail":0,"footnotes":""},"categories":[65],"tags":[71],"acf":[],"_links":{"self":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts\/25381"}],"collection":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/comments?post=25381"}],"version-history":[{"count":0,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts\/25381\/revisions"}],"wp:attachment":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/media?parent=25381"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/categories?post=25381"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/tags?post=25381"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}