{"id":25383,"date":"2018-12-10T08:36:15","date_gmt":"2018-12-10T07:36:15","guid":{"rendered":"http:\/\/help.qualiware.it\/qw-help\/?p=25383"},"modified":"2020-10-11T11:05:28","modified_gmt":"2020-10-11T09:05:28","slug":"webservice-invio-di-dati-tramite-un-servizio-webapi-rest","status":"publish","type":"post","link":"https:\/\/help.qualiware.it\/qw-help\/webservice-invio-di-dati-tramite-un-servizio-webapi-rest\/","title":{"rendered":"WebService: invio di dati tramite 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) per inviare dati in formato Json\r\n\r\nDim url As String = \"http:\/\/192.168.0.244:8999\/api\/UDC\/VersaDaProduzione\"\r\n\r\nDim webRequest__1 As Net.HttpWebRequest = DirectCast(Net.WebRequest.Create(url), Net.HttpWebRequest)\r\nwebRequest__1.Method = \"POST\"\r\nwebRequest__1.ContentType = \"application\/json\"\r\nwebRequest__1.Accept = \"application\/json\"\r\n\r\n' Creazione del JSON di invio dei dati\r\ndim json as string\r\n\r\njson = \"{\"\r\njson += \"\"\"BrcArt\"\": \"\"\" +form.findcontrol(\"BARCODE\").Value + \"\"\",\"\r\njson += \"\"\"UbcOrig\"\": \"\"\" +form.findcontrol(\"UBCORIG\").Value + \"\"\",\"\r\njson += \"\"\"UbcDest\"\": \"\"\" +form.findcontrol(\"UBCDEST\").Value + \"\"\",\"\r\njson += \"\"\"UdcDest\"\": \"\"\" +form.findcontrol(\"UDCDEST\").Value + \"\"\",\"\r\njson += \"\"\"Qta\"\": \" + cstr(form.findcontrol(\"QTA\").Value) + \",\"\r\njson += \"\"\"IdDms\"\": \" + cstr(form.findcontrol(\"IDDMS\").Value) + \",\"\r\njson += \"\"\"Altezza\"\": \" + cstr(form.findcontrol(\"ALTEZZA\").Value)\r\njson += \"}\"\r\n\r\n' 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\r\nwebRequest__1.ContentLength = json.ToString().Length\r\n\r\nUsing requestWriter2 As New IO.StreamWriter(webRequest__1.GetRequestStream())\r\n      requestWriter2.Write(json)\r\nEnd Using\r\n\r\n'Recupero della risposta\r\nDim ok as boolean = true\r\n\r\ndim resp As Net.HttpWebResponse \r\n\r\ntry\r\n\tresp = DirectCast(webRequest__1.GetResponse(), Net.HttpWebResponse)\r\n\r\n\tif resp.StatusCode = 200 then\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 jsonr As String = sr.ReadToEnd() ' This reads the JSon with the result\r\n\t\t\r\n\t\tform.Alert(jsonr)\r\n\t\t\r\n\t\tform.findcontrol(\"GIA_SPED\").Value = true\r\n\t\tform.Save()\r\n\tend if\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\tEnd Using \r\nend try\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>&#8216; In questo esempio viene effettuata la chiamata ad un servizio WebApi REST (webMethod) per inviare dati in formato Json Dim url As String = &#8220;http:\/\/192.168.0.244:8999\/api\/UDC\/VersaDaProduzione&#8221; Dim webRequest__1 As Net.HttpWebRequest = DirectCast(Net.WebRequest.Create(url), Net.HttpWebRequest) webRequest__1.Method = &#8220;POST&#8221; webRequest__1.ContentType = &#8220;application\/json&#8221; webRequest__1.Accept = &#8220;application\/json&#8221; &#8216; Creazione del JSON di invio dei dati dim json as string json&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\/25383"}],"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=25383"}],"version-history":[{"count":0,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/posts\/25383\/revisions"}],"wp:attachment":[{"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/media?parent=25383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/categories?post=25383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/help.qualiware.it\/qw-help\/wp-json\/wp\/v2\/tags?post=25383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}