Post JSON 数据到 vb.net 中的网络服务器使用 JSON.Net
Post JSON data to web server in vb.net using JSON.Net
我正在尝试将 post 数据以 JSON 形式发送到网站,但无论我做什么,我都会收到 500 内部错误。
我打算创建的字符串是这样的:{"orders":[{"id":"7","invoice_number":"1007"},{"id":"8","invoice_number":"1008"},{"id":"11","invoice_number":"1011"}]} ,我可以用 JSON.net
站点的网站管理员向我发送了此命令,以便 post 数据
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
但我不知道什么是 curl 以及这与我所做的有何不同。
这是我的代码:
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
的class
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
curl 是一个用于传输网络数据的命令行工具。这是手册页:man.cx/curl.
给定的 curl 示例中的 -X 选项表示使用 GET 以外的其他内容。在这种情况下,他们期待 PATCH。试试 req.Method = PATCH
.
我之前没有运行进入 PATCH 方法,RFC 摘要让它听起来像一个 HTTP upsert:
The PATCH method requests that a set of changes described in the
request entity be applied to the resource identified by the Request-
URI. The set of changes is represented in a format called a "patch
document" identified by a media type. If the Request-URI does not
point to an existing resource, the server MAY create a new resource,
depending on the patch document type (whether it can logically modify
a null resource) and permissions, etc.
我正在尝试将 post 数据以 JSON 形式发送到网站,但无论我做什么,我都会收到 500 内部错误。
我打算创建的字符串是这样的:{"orders":[{"id":"7","invoice_number":"1007"},{"id":"8","invoice_number":"1008"},{"id":"11","invoice_number":"1011"}]} ,我可以用 JSON.net
站点的网站管理员向我发送了此命令,以便 post 数据
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
但我不知道什么是 curl 以及这与我所做的有何不同。
这是我的代码:
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
的classPublic 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
curl 是一个用于传输网络数据的命令行工具。这是手册页:man.cx/curl.
给定的 curl 示例中的 -X 选项表示使用 GET 以外的其他内容。在这种情况下,他们期待 PATCH。试试 req.Method = PATCH
.
我之前没有运行进入 PATCH 方法,RFC 摘要让它听起来像一个 HTTP upsert:
The PATCH method requests that a set of changes described in the request entity be applied to the resource identified by the Request- URI. The set of changes is represented in a format called a "patch document" identified by a media type. If the Request-URI does not point to an existing resource, the server MAY create a new resource, depending on the patch document type (whether it can logically modify a null resource) and permissions, etc.