使用JSON.Net将JSON数据发布到vb.net中的Web服务器

时间:2015-03-20 15:39:57

标签: json vb.net json.net

我正在尝试以JSON格式将数据发布到网站,但无论我做什么,我都会收到500内部错误。

我打算创建的字符串是这样的:{“orders”:[{“id”:“7”,“invoice_number”:“1007”},{“id”:“8”,“invoice_number” :“1008”},{“id”:“11”,“invoice_number”:“1011”}]},我可以通过JSON.net轻松获取

网站站长向我发送了此命令以发布数据

curl -vvvvvvvv "http://staging.voltige2001.net/fr/api/update-orders" --data '{"orders":[{"id":"7","invoice_number":"1007"},{"id":"8","invoice_number":"1008"},{"id":"11","invoice_number":"1011"}]}' -X PATCH

但我不知道什么是卷曲,这与我正在做的有什么不同。

这是我的代码:

    Dim strInvNumber As String

    If Not IsNothing(oDsFacture) AndAlso oDsFacture.Tables.Count > 0 AndAlso oDsFacture.Tables(0).Rows.Count > 0 Then
        strInvNumber = oDsFacture.Tables(0).Rows(0)("No_Facture")
    Else
        strInvNumber = "9999" ' Pas de facture
    End If

    Dim oOrder As New Confirmation.Order With {.ID = oCommande.ID, .InvoiceNumber = strInvNumber}

    Dim oConfirmation As New Confirmation With {.Orders = New List(Of Confirmation.Order) From {oOrder}}
    Dim strResponse As String = JsonConvert.SerializeObject(oConfirmation)

    Dim data = Encoding.UTF8.GetBytes(strResponse)


    Dim req As WebRequest = WebRequest.Create(_ResponseURL)
    req.ContentType = "application/json"
    req.Method = "POST"
    'req.ContentLength = strResponse.Length

    Using oStream As New StreamWriter(req.GetRequestStream)
        oStream.Write(strResponse)
        oStream.Flush()
        oStream.Close()
    End Using

    Try
        Dim response As HttpWebResponse = req.GetResponse
        Using oSReader As New StreamReader(response.GetResponseStream)

        End Using
    Catch ex As Exception

    End Try

这是我用来转换为JSON的类

Public Class Confirmation


Public Class Order
    <JsonProperty("id")> Public Property ID As String
    <JsonProperty("invoice_number")> Public Property InvoiceNumber As String
        End Class

<JsonProperty("orders")> Public Property Orders As List(Of Order)
End Class

1 个答案:

答案 0 :(得分:1)

curl是一种用于传输网络数据的命令行工具。这是手册页:man.cx/curl

您给出的curl示例中的-X选项意味着使用GET之外的其他内容。在这种情况下,他们期待PATCH。试试req.Method = PATCH

我之前没有遇到过PATCH方法,RFC摘要让它听起来像是一个HTTP upsert:

  

PATCH方法请求中描述的一组更改      请求实体应用于请求所标识的资源 -      URI。这组更改以称为“补丁”的格式表示      文档“由媒体类型标识。如果Request-URI没有      指向现有资源,服务器可以创建新资源,      取决于补丁文档类型(是否可以在逻辑上修改      空资源)和权限等。