如何将 'version' 字段添加到 Content-Type Header 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 版本: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")

这样,它就像一个魅力。