【问题标题】:Invalid content type format in c# http requestc# http请求中的内容类型格式无效
【发布时间】:2018-08-15 19:39:21
【问题描述】:

我正在尝试设置这样的内容类型:

System.FormatException: '值'multipart/form-data;boundary=ce4da8a9-0e0d-4ba6-9f00-e7f3b002a717'的格式无效。'

执行时

HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, uri)
{
    Content = new StringContent(postData, Encoding.UTF8, content_type) // CONTENT-TYPE header
};

但是按照这个

https://docs.microsoft.com/en-us/skype-sdk/ucwa/batchingrequests https://ucwa.skype.com/documentation/Resources-batch-3 https://ucwa.skype.com/documentation/ProgrammingConcepts-Batch

所有资源都说要使用我拥有的格式。它也没有在 Postman 中抱怨这一点。有没有办法可以在 c# 中的 StringContent 中强制使用这种内容类型?

这里有答案

How do I send arbitrary JSON data, with a custom header, to a REST server?

但我不确定如何实现。

【问题讨论】:

  • 我尝试从链接中输入答案,但它显示__DynamicallyInvokable 未定义。

标签: c# http


【解决方案1】:

从错误消息中可以看出答案s/b。 "multipart/form-data;boundary=ce4da8a9-0e0d-4ba6-9f00-e7f3b002a717" 不是有效的内容类型。即使您正在创建自定义内容类型,也禁止使用以下字符:

( ) @ < > , ; : \ " / [ ] ? = { }

在您的内容类型中存在任何这些都会导致

StringContent(postData, Encoding.UTF8, content_type)

System.FormatException 失败

【讨论】:

  • 我怎样才能附加这个自定义内容类型呢?如果我可以在邮递员中发送此内容,那么 c# 中应该有办法吗?
  • 我不认为边界信息应该是内容类型的一部分。
猜你喜欢
  • 2012-04-07
  • 1970-01-01
  • 1970-01-01
  • 2014-10-26
  • 2020-10-21
  • 1970-01-01
  • 2020-03-26
  • 2019-07-08
  • 2015-02-18
相关资源
最近更新 更多