【问题标题】:How to add 'version' field to Content-Type Header in VB.NET如何在 VB.NET 中将“版本”字段添加到 Content-Type Header
【发布时间】:2022-01-13 10:05:31
【问题描述】:

我正在为我的 Visual Basic .Net 应用程序实现与 API REST 的通信。当我尝试将字段 version=1 添加到 Content-Type 标头时,会出现此问题。这是我用来执行此操作的代码:

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 标头中包含版本字段,他们是这样说的: 带有版本的内容类型:application/vnd.gestiona.third+json; version=1

关于如何解决这个问题的任何想法?

感谢您的阅读

【问题讨论】:

  • 使用application/json 而不是application/vnd.gestiona.third+json; version=1
  • 嗨@RajdipBusa,感谢您回答我的问题。我试过你说的,但我得到一个 System.FormatException 错误,更具体地说:MediaTypeHeaderValue.CheckMediaTypeFormat(String mediaType, String parameterName) MediaTypeHeaderValue.ctor(String mediaType)
  • 你试过text/plain吗(仅供实验)
  • @RajdipBusa 是的,我试过了,它没有返回错误(我想这是正确的),问题是我需要在 Content-Type 标头上添加 version 字段.无论如何,感谢您的宝贵时间。

标签: vb.net api rest http http-headers


【解决方案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")

这样,它就像一个魅力。

【讨论】:

    猜你喜欢
    • 2018-05-28
    • 2012-05-14
    • 2010-12-08
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-06
    • 2016-01-30
    相关资源
    最近更新 更多