![](/img/trans.png)
[英]Send a POST request of JSON content-type and display JSON response in VB.NET
[英]How to add 'version' field to Content-Type Header in VB.NET
我正在為我的 Visual Basic.Net 應用程序實現與 API REST 的通信。 當我嘗試將字段version=1
添加到Content-Type
header 時,會出現此問題。 這是我用來執行此操作的代碼:
Public Function AddTercero(tercero As Tercero, connection As GestionaConnection) As Boolean
If connection Is Nothing Then
Throw New ArgumentNullException(NameOf(connection))
End If
If tercero Is Nothing Then
Throw New ArgumentNullException(NameOf(tercero))
End If
Dim request = New HttpRequestMessage()
request.RequestUri = New Uri(Convert.ToString(connection.RecursosDictionary("vnd.gestiona.thirds"), CultureInfo.CurrentCulture))
request.Method = HttpMethod.Post
request.Headers.Add("X-Gestiona-Access-Token", AccessToken)
request.Headers.Add("Accept", "application/json")
request.Content = New StringContent(JsonConvert.SerializeObject(tercero))
request.Content.Headers.ContentType = New MediaTypeHeaderValue("application/vnd.gestiona.third+json; version=1")
Dim req = request.Content.ReadAsStringAsync
Dim response As HttpResponseMessage = connection.Connection.SendAsync(request).Result
request.Dispose()
Dim resultado = response.Content.ReadAsStringAsync()
Debug.WriteLine(resultado.Result.ToString)
If response.StatusCode = HttpStatusCode.Created Then
Return True
ElseIf response.StatusCode = HttpStatusCode.Unauthorized Then
Throw New InvalidOperationException("Error al añadir tercero: no tiene autorización " & response.ReasonPhrase)
Else
Throw New InvalidOperationException("Error al añadir tercero: " & response.StatusCode & " -> " & response.ReasonPhrase)
End If
End Function
錯誤消息說:
System.InvalidOperationException: Error al añadir tercero: 415 -> Unsupported Media Type
我從服務器得到的消息是:
{
"code": 415,
"name": "Unsupported Media Type",
"description": "Content not supported: application/vnd.gestiona.third+json",
"technical_details": "http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html#sec10.4.16"
}
我已經與 API 的開發人員進行了交談,他們說我必須在 Content-Type header 上包含版本字段,這就是他們所說的:Content-Type with version: application/vnd.gestiona.third+json; version=1
application/vnd.gestiona.third+json; version=1
關於如何解決這個問題的任何想法?
感謝您閱讀
我通過分別聲明application/vnd.gestiona.third+json
內容類型和version=1
類型來解決它,如下所示:
request.Content.Headers.ContentType = New MediaTypeHeaderValue("application/vnd.gestiona.third+json")
request.Content.Headers.ContentType.Parameters.Add(New NameValueHeaderValue("version", "1"))
而不是像我那樣做:
request.Content.Headers.ContentType = New MediaTypeHeaderValue("application/vnd.gestiona.third+json; version=1")
這樣,它就像一個魅力。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.