C# 在c中使用json发布请求#

C# 在c中使用json发布请求#,c#,json,post,C#,Json,Post,我试图做一个POST请求,我已经通过邮递员工作,但我得到一个400错误时,从代码完成 我正试图从这里到达POST端点: 这是我的代码,有没有什么不正确的地方,或者我遗漏了什么 public void MakeOrder(string UID) { string url = $"https://api-fxpractice.oanda.com/v3/accounts/{UID}/orders"; string body = "{'order': {'unit

我试图做一个POST请求,我已经通过邮递员工作,但我得到一个400错误时,从代码完成

我正试图从这里到达POST端点:

这是我的代码,有没有什么不正确的地方,或者我遗漏了什么

public void MakeOrder(string UID)
    {
        string url = $"https://api-fxpractice.oanda.com/v3/accounts/{UID}/orders";
        string body = "{'order': {'units': '10000', 'instrument': 'EUR_USD', 'timeInForce': 'FOK', 'type': 'MARKET', 'positionFill': 'DEFAULT'}}";
        using (WebClient client = new WebClient())
        {
            client.Headers.Add("Authorization", "Bearer 11699873cb44ea6260ca3aa42d2898ac-2896712134c5924a25af3525d3bea9b0");
            client.Headers.Add("Content-Type", "application/json");
            client.UploadString(url, body);
        }
    }

我对编码非常陌生,因此如果它非常简单,我深表歉意。

使用System.Net.Http中的HttpClient:

using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;

using (var httpClient = new HttpClient())
{

       string url = $"https://api-fxpractice.oanda.com/v3/accounts/{UID}/orders";
       string body = "{'order': {'units': '10000', 'instrument': 'EUR_USD', 'timeInForce': 'FOK', 'type': 'MARKET', 'positionFill': 'DEFAULT'}}";
       var content = new StringContent(body , Encoding.UTF8, "application/json");
       var result = httpClient.PostAsync(url, content).Result;
       var contents = result.Content.ReadAsStringAsync();
}

从System.Net.Http使用HttpClient:

using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;

using (var httpClient = new HttpClient())
{

       string url = $"https://api-fxpractice.oanda.com/v3/accounts/{UID}/orders";
       string body = "{'order': {'units': '10000', 'instrument': 'EUR_USD', 'timeInForce': 'FOK', 'type': 'MARKET', 'positionFill': 'DEFAULT'}}";
       var content = new StringContent(body , Encoding.UTF8, "application/json");
       var result = httpClient.PostAsync(url, content).Result;
       var contents = result.Content.ReadAsStringAsync();
}

我建议您通过WebClient使用
HttpClient
。你可以找到区别

using (var httpClient = new HttpClient())
{

    string url = $"https://api-fxpractice.oanda.com/v3/accounts/{UID}/orders";
    string body = "{'order': {'units': '10000', 'instrument': 'EUR_USD', 'timeInForce': 'FOK', 'type': 'MARKET', 'positionFill': 'DEFAULT'}}";
    var content = new StringContent(body, Encoding.UTF8, "application/json");
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1OTIzMDY0NTQsImlzcyI6IlRlc3QuY29tIiwiYXVkIjoiVGVzdC5jb20ifQ.c-3boD5NtOEhXNUnzPHGD4rY1lbEd-pjfn7C6kDPbxw");
    var result = httpClient.PostAsync(url, content).Result;
    var contents = result.Content.ReadAsStringAsync();
}

我建议您通过WebClient使用
HttpClient
。你可以找到区别

using (var httpClient = new HttpClient())
{

    string url = $"https://api-fxpractice.oanda.com/v3/accounts/{UID}/orders";
    string body = "{'order': {'units': '10000', 'instrument': 'EUR_USD', 'timeInForce': 'FOK', 'type': 'MARKET', 'positionFill': 'DEFAULT'}}";
    var content = new StringContent(body, Encoding.UTF8, "application/json");
    httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJleHAiOjE1OTIzMDY0NTQsImlzcyI6IlRlc3QuY29tIiwiYXVkIjoiVGVzdC5jb20ifQ.c-3boD5NtOEhXNUnzPHGD4rY1lbEd-pjfn7C6kDPbxw");
    var result = httpClient.PostAsync(url, content).Result;
    var contents = result.Content.ReadAsStringAsync();
}

使用这个,我似乎仍然得到了400个错误错误的请求。我已经用我的API测试了代码,它在我的API中运行良好。所以我觉得你发布的数据有点问题。你能给我看看邮递员吗?您正在发送的数据和标题以及2个自定义标题:授权:持有者11699873cb44ea6260ca3aa42d2898ac-2896712134c5924a25af3525d3bea9b0内容类型:应用程序/json正文:{“订单”:{“单位”:“100”,“仪器”:“欧元美元”,“时间信息”:“FOK”,“类型”:“市场”,“位置填充”:“默认值”}只有一个标题?没有内容类型?使用这个,我似乎仍然得到了400个错误-错误的请求。我已经用我的API测试了代码,它可以用我的API正常工作。所以我觉得你发布的数据有点问题。你能给我看看邮递员吗?您正在发送的数据和标题以及2个自定义标题:授权:持有者11699873cb44ea6260ca3aa42d2898ac-2896712134c5924a25af3525d3bea9b0内容类型:应用程序/json正文:{“订单”:{“单位”:“100”,“仪器”:“欧元美元”,“时间信息”:“FOK”,“类型”:“市场”,“位置填充”:“默认值”}只有一个标题?没有内容类型?不幸的是,即使添加了授权头,我仍然收到一个400错误:/string body对您来说正确吗?您是否尝试过使用“而不是”的json?有时像这样的小规范可能会有效果。不要忘了您需要在字符串中转义\“刚试过,但不幸的是我还是犯了同样的错误。在OANDAAPI指令中,它表示Header-Accept Datetime格式中需要以下参数,我不必向Postman中输入任何类似的内容,只需输入授权和application/json即可。你认为这可能与此有关吗?不幸的是,即使添加了授权头,我仍然收到一个400错误:/string body在你看来正确吗?你是否尝试过使用“而不是”的json?有时像这样的小规范会有效果。别忘了你需要在字符串中转义“作为\“刚试过,但不幸的是我还是犯了同样的错误。在OANDAAPI指令中,它表示Header-Accept Datetime格式中需要以下参数,我不必向Postman中输入任何类似的内容,只需输入授权和application/json即可。你认为这可能与此有关吗?