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

WPF/C#:POST API使用变量时请求体格式错误求助

问题解决:Okta POST请求体格式错误(ErrorCode E000003)

错误原因

你手动拼接JSON请求体时,字符串类型的username和password没有被双引号包裹,导致生成的JSON格式不合法,触发了Okta的请求体格式错误提示。

比如原代码生成的JSON是:

{"username":testuser,"password":testpass,"options": {"multiOptionalFactorEnroll": true,"warnBeforePasswordExpired": true}}

而正确的JSON要求字符串值必须用双引号包裹:

{"username":"testuser","password":"testpass","options": {"multiOptionalFactorEnroll": true,"warnBeforePasswordExpired": true}}

解决方案

方法1:修正字符串拼接(快速修复)

直接在变量前后添加双引号,用字符串插值简化拼接:

var OktaUserName = ADAccountName;
var OktaPassword = Password;

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://trial.okta.com/api/v1/authn");

// 给username和password变量添加双引号
var jsonBody = $"{{\"username\":\"{OktaUserName}\",\"password\":\"{OktaPassword}\",\"options\": {{\"multiOptionalFactorEnroll\": true,\"warnBeforePasswordExpired\": true}}}}";
request.Content = new StringContent(jsonBody, Encoding.UTF8, "application/json");

var response = await client.SendAsync(request);
var responseContent = await response.Content.ReadAsStringAsync();
// 原代码中"json"变量未定义,这里修正为responseContent
MessageBox.Show(responseContent, "Alert", MessageBoxButton.OK);
MessageBox.Show(responseContent, "Message");

方法2:使用强类型序列化(推荐,避免格式错误)

通过定义对应JSON结构的类,用序列化工具生成合法JSON,更易维护且不易出错:

  1. 定义请求体对应的类:
public class OktaAuthRequest
{
    [JsonPropertyName("username")]
    public string Username { get; set; }
    
    [JsonPropertyName("password")]
    public string Password { get; set; }
    
    [JsonPropertyName("options")]
    public OktaAuthOptions Options { get; set; } = new OktaAuthOptions();
}

public class OktaAuthOptions
{
    [JsonPropertyName("multiOptionalFactorEnroll")]
    public bool MultiOptionalFactorEnroll { get; set; } = true;
    
    [JsonPropertyName("warnBeforePasswordExpired")]
    public bool WarnBeforePasswordExpired { get; set; } = true;
}
  1. 在业务代码中使用序列化生成请求体:
var OktaUserName = ADAccountName;
var OktaPassword = Password;

// 建议HttpClient全局复用,不要每次请求都创建新实例
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, "https://trial.okta.com/api/v1/authn");

// 构建请求对象
var authRequest = new OktaAuthRequest
{
    Username = OktaUserName,
    Password = OktaPassword
};

// 序列化生成合法JSON
var jsonContent = System.Text.Json.JsonSerializer.Serialize(authRequest);
request.Content = new StringContent(jsonContent, Encoding.UTF8, "application/json");

var response = await client.SendAsync(request);
var responseContent = await response.Content.ReadAsStringAsync();
MessageBox.Show(responseContent, "Alert", MessageBoxButton.OK);
MessageBox.Show(responseContent, "Message");

额外注意事项

  • HttpClient建议全局复用,避免频繁创建新实例导致socket资源耗尽。
  • 原代码中MessageBox.Show("Alert", json, "OK");里的json变量未定义,需替换为实际的响应内容变量。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.12 16:55:22