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

ASP.NET WebForms(C#)调用PlaceOrder API遇400错误求解决方案

ASP.NET WebForms(C#)调用PlaceOrder API的正确方式

首先明确目标API接受application/x-www-form-urlencoded格式的POST请求,参数包含两个必填字段:

  • jData:值为JSON格式字符串,包含uid、actid、qty等指定字段
  • jKey:登录成功后获取的有效密钥

以下是两种经过验证的调用方式:

方式一:使用HttpClient(推荐)

using System;
using System.Net.Http;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Web.Script.Serialization; // 也可替换为Newtonsoft.Json

protected async void PlaceOrder_Click(object sender, EventArgs e)
{
    // 构造jData对应的实体对象
    var orderParams = new
    {
        uid = "Test",
        actid = "Test",
        qty = "50",
        price = "1400",
        ret = "DAY"
    };

    // 序列化为JSON字符串
    string jData = new JavaScriptSerializer().Serialize(orderParams);
    // 若使用Newtonsoft.Json,替换为:string jData = JsonConvert.SerializeObject(orderParams);

    // 构建表单参数集合
    var formData = new Dictionary<string, string>
    {
        { "jData", jData },
        { "jKey", "TRELIU34533456SSUDFGU234" } // 替换为实际有效密钥
    };

    using (var httpClient = new HttpClient())
    {
        try
        {
            var content = new FormUrlEncodedContent(formData);
            var response = await httpClient.PostAsync("https://connect.mycomp.in/ConnectTP/PlaceOrder", content);

            if (response.IsSuccessStatusCode)
            {
                string result = await response.Content.ReadAsStringAsync();
                // 处理接口返回的正常结果
            }
            else
            {
                string errorDetail = await response.Content.ReadAsStringAsync();
                // 读取Bad Request的具体错误信息
            }
        }
        catch (Exception ex)
        {
            // 处理网络或其他异常
        }
    }
}

方式二:使用WebClient

using System;
using System.Net;
using System.Collections.Specialized;
using System.Web.Script.Serialization;

protected void PlaceOrder_Click(object sender, EventArgs e)
{
    var orderParams = new
    {
        uid = "Test",
        actid = "Test",
        qty = "50",
        price = "1400",
        ret = "DAY"
    };

    string jData = new JavaScriptSerializer().Serialize(orderParams);

    using (var webClient = new WebClient())
    {
        try
        {
            webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
            
            var formData = new NameValueCollection
            {
                { "jData", jData },
                { "jKey", "TRELIU34533456SSUDFGU234" }
            };

            byte[] responseBytes = webClient.UploadValues("https://connect.mycomp.in/ConnectTP/PlaceOrder", "POST", formData);
            string result = System.Text.Encoding.UTF8.GetString(responseBytes);
            // 处理返回结果
        }
        catch (WebException ex)
        {
            // 获取错误响应的详细内容
            using (var reader = new System.IO.StreamReader(ex.Response.GetResponseStream()))
            {
                string errorMsg = reader.ReadToEnd();
                // 处理错误信息
            }
        }
    }
}

常见错误排查

  • Content-Type设置错误:必须设为application/x-www-form-urlencoded,不要误用application/json
  • 参数传递方式错误:不能直接将JSON作为请求体发送,需将JSON字符串放入jData表单字段,与jKey一起提交
  • JSON序列化问题:确保序列化后的字符串格式合法,无语法错误
  • 密钥有效性:确认jKey是登录后获取的有效密钥,未过期且权限匹配

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.22 09:15:31