You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

指定application/json仍报Unsupported media type错误求助

解决C# ASP.NET调用JSON-RPC API时的"Unsupported media type error"问题

核心排查方向

这个错误几乎都是因为请求的Content-Type设置不符合API要求,或者JSON请求体格式与Postman发送的不一致导致的。以下是具体的解决步骤:

1. 确保正确创建StringContent对象

很多时候问题出在StringContent的构造参数顺序或Content-Type的指定上,正确的写法应该是:

// 假设jsonStr是你要发送的JSON字符串
var content = new StringContent(jsonStr, Encoding.UTF8, "application/json");

注意:

  • 第三个参数必须严格是application/json,不要添加多余的后缀(比如;charset=utf-8),部分API对Content-Type的匹配要求严格。
  • 必须指定Encoding.UTF8,避免编码不一致导致的解析失败。

2. 用序列化类生成JSON(避免手动拼接错误)

手动拼接JSON容易出现格式错误(比如多余逗号、引号不匹配),建议用强类型类序列化生成请求体,和Postman的格式完全对齐:

定义请求体类

// 用Newtonsoft.Json的话,需要安装Newtonsoft.Json NuGet包
public class JsonRpcRequest
{
    [Newtonsoft.Json.JsonProperty("jsonrpc")]
    public string JsonRpc { get; set; } = "2.0";
    
    [Newtonsoft.Json.JsonProperty("method")]
    public string Method { get; set; }
    
    [Newtonsoft.Json.JsonProperty("params")]
    public Dictionary<string, string> Params { get; set; }
    
    [Newtonsoft.Json.JsonProperty("id")]
    public string Id { get; set; }
}

// 如果用System.Text.Json(.NET Core 3.0+内置)
public class JsonRpcRequest
{
    [System.Text.Json.Serialization.JsonPropertyName("jsonrpc")]
    public string JsonRpc { get; set; } = "2.0";
    
    [System.Text.Json.Serialization.JsonPropertyName("method")]
    public string Method { get; set; }
    
    [System.Text.Json.Serialization.JsonPropertyName("params")]
    public Dictionary<string, string> Params { get; set; }
    
    [System.Text.Json.Serialization.JsonPropertyName("id")]
    public string Id { get; set; }
}

完整请求示例代码

using System.Net.Http;
using System.Text;
// Newtonsoft.Json版本
using Newtonsoft.Json;
// 或者System.Text.Json版本
// using System.Text.Json;

public async Task<string> CallLicenseApi()
{
    using var httpClient = new HttpClient();
    var apiUrl = "你的API地址";
    
    // 构造请求体
    var requestBody = new JsonRpcRequest
    {
        Method = "getLicenseInfo",
        Id = "620e91a211b42421733aa2b4",
        Params = new Dictionary<string, string>
        {
            { "companyId", "620e91a211b42421733aa2b4" }
        }
    };
    
    // 序列化JSON
    string json;
    // Newtonsoft.Json方式
    json = JsonConvert.SerializeObject(requestBody);
    // System.Text.Json方式(需要配置属性名匹配)
    // var options = new JsonSerializerOptions { PropertyNamingPolicy = null }; // 保持字段名原样
    // json = JsonSerializer.Serialize(requestBody, options);
    
    // 创建请求内容
    var content = new StringContent(json, Encoding.UTF8, "application/json");
    
    // 发送请求
    var response = await httpClient.PostAsync(apiUrl, content);
    
    // 检查响应状态
    if (!response.IsSuccessStatusCode)
    {
        // 可以打印响应内容排查
        var errorContent = await response.Content.ReadAsStringAsync();
        throw new HttpRequestException($"请求失败:{response.StatusCode},错误内容:{errorContent}");
    }
    
    return await response.Content.ReadAsStringAsync();
}

3. 对比Postman和C#的请求细节

用Fiddler、Charles或HttpClient日志工具,对比两者的:

  • 请求头:确保Content-Type完全一致,同时检查Accept头是否和Postman相同(部分API会校验Accept)。
  • 请求体:把C#序列化后的JSON和Postman里的复制出来对比,确保没有格式差异(比如缩进、空格、字段顺序,虽然JSON规范不要求顺序,但极少数API会校验)。

4. 额外注意事项

  • 如果API要求JSON不能有缩进,可以在序列化时关闭缩进:
    // Newtonsoft.Json
    json = JsonConvert.SerializeObject(requestBody, Formatting.None);
    // System.Text.Json
    var options = new JsonSerializerOptions { WriteIndented = false };
    json = JsonSerializer.Serialize(requestBody, options);
    
  • 避免在请求中添加多余的自定义头,除非API明确要求。

内容的提问来源于stack exchange,提问作者ilsologheo00

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 08:01:27