首页 > 解决方案 > c# http请求中的内容类型格式无效

问题描述

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

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/编程概念-批处理

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

这里有答案

如何将带有自定义标头的任意 JSON 数据发送到 REST 服务器?

但我不确定如何实现这一点。

标签: c#http

解决方案


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

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

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

StringContent(postData, Encoding.UTF8, content_type)

失败System.FormatException


推荐阅读