Necesitas referenciar a la dll Newtonsoft.Json.dll http://www.newtonsoft.com/json para deserialización de la respuesta al servicio (en json)
'Llamada post a: http://api.test.es/v1/post_insert_value para insertar los valores value1, value2, value3
'Llamada post a: http://api.test.es/v1/post_insert_value para insertar los valores value1, value2, value3
'Respuesta esperada:
{"data":{"has_result":"yes"}}
Public Class ContainerInsertResult
Public
data As InsertResult
End Class
Public Class InsertResult
Public
has_result As String
End Class
Sub post()
Try
Dim request As WebRequest = WebRequest.Create("http://api.test.es/v1/post_insert_value")
request.Method = "POST"
Dim postData As String = _
"&value1=test" & _
"&value2=test2" & _
"&value3=test3"
Dim byteArray As Byte() = Encoding.UTF8.GetBytes(postData)
request.ContentType = "application/x-www-form-urlencoded"
Dim response As WebResponse
Dim dataStream As Stream
dataStream = request.GetRequestStream()
dataStream.Write(byteArray, 0, byteArray.Length)
dataStream.Close()
response = request.GetResponse()
dataStream = response.GetResponseStream()
Dim reader As New StreamReader(dataStream)
Dim responseFromServer As
String = reader.ReadToEnd()
Dim parsedResponse As
ContainerInsertResult = JsonConvert.DeserializeObject(Of ContainerInsertResult)(responseFromServer)
Dim respuesta As InsertResult = parsedResponse.data
MsgBox(respuesta.has_result)
Catch ex As WebException
If
(ex.Status = WebExceptionStatus.ProtocolError)
Then
Dim
responsee As WebResponse
= ex.Response
Using
(responsee)
Dim
httpResponse As HttpWebResponse
= CType(responsee, HttpWebResponse)
Dim statusCode As HttpStatusCode
= httpResponse.StatusCode
Dim myStreamReader As StreamReader = New StreamReader(responsee.GetResponseStream())
Using (myStreamReader)
Dim ResponseText As String = myStreamReader.ReadToEnd
MessageBox.Show(ResponseText)
End Using
End Using
End If
End Try
End Sub