【问题标题】:C# HttpClient post content with FormUrlEncodedContent object in Dictionary string/objectC# HttpClient 在字典字符串/对象中使用 FormUrlEncodedContent 对象发布内容
【发布时间】:2019-10-21 17:13:16
【问题描述】:

我正在尝试向我的服务器发布一个联系人。 这就是我过去一直这样做的方式,直到我不得不使用字符串以外的对象。

using (HttpClient client = new HttpClient())
{
    client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(authType, tokens);
    var postParams = new Dictionary<string, object>();

    postParams.Add("string", string);
    postParams.Add("int", string);
    postParams.Add("datetime", DateTime);
    postParams.Add("datetime", DateTime);
    postParams.Add("Match", Match);
    postParams.Add("TicketId", token);

    using (var postContent = new FormUrlEncodedContent(postParams.ToDictionary()))
    {
        var myContent = JsonConvert.SerializeObject(postParams);
        var buffer = System.Text.Encoding.UTF8.GetBytes(myContent);
        var byteContent = new ByteArrayContent(buffer);
        byteContent.Headers.ContentType = new MediaTypeHeaderValue("application/json");

        using (HttpResponseMessage response = await client.PostAsync(@"http://url/api", byteContent))
        {
            response.EnsureSuccessStatusCode(); // Throw if httpcode is an error
            using (HttpContent content = response.Content)
            {
                string result = await content.ReadAsStringAsync();
                var Json = JsonConvert.DeserializeObject<bool>(result);
                return Json;
            }
        }
    }
}

这就是我的请求应该是这样的。

methode: POST
object: {
    "title":"test-ticket-2",
    "detail": "Description test create ticket in prod",
    "dateStart": "2019-10-06",
    "dateEnd": "2019-10-12",
    "ratio": "2.15",
    "matchResult": "2",
    "matchs": [
            {
                "Teams": "Test-match-1",
                "Proposal": "3x",
                "DateStart": "2019-10-06 18:00",
                "DateEnd": "2019-10-06 20:00",
                "Payout": "0.6"
            }
             ]

我不知道是否以及如何添加字符串以外的对象并发出请求。 有什么想法吗?

编辑:Match 看起来像这样

public class Match
{
    public int Id { get; set; }
    public string Teams { get; set; }
    public string MatchResults { get; set; }
    public string Proposal { get; set; }
    public string Payout { get; set; }
    public DateTime? DateStart { get; set; }
    public DateTime? DateEnd { get; set; }
    public Uri Ball { get; set; }
    public int TicketId { get; set; }
}

【问题讨论】:

标签: c# json http encoding httpclient


【解决方案1】:

我如何添加字符串以外的对象并提出请求。任何 想法?

using (HttpClient httpclient = new HttpClient()) 
{
    Models.ApplicationUser applicationUser = new ApplicationUser();
    string serialized = Newtonsoft.Json.JsonConvert.SerializeObject(applicationUser);
    StringContent stringContent = new StringContent(serialized);
    httpclient.PostAsync("url", stringContent);
}

希望你想做这样的事情

【讨论】:

  • 我猜是的,但它不是一个对象。它是字符串和用户类的混合体。
  • @Alejandro - 您是否尝试过添加postParams.Add("matchs", new List&lt;Match&gt; { /* Contents of the list */ }); 并使用您预先存在的代码,记住var postContent = new FormUrlEncodedContent(postParams.ToDictionary()) 从未使用过,因此几乎可以肯定会被跳过?
  • @alejandro。它仍然可以工作。将字符串和类保存在另一个类中
  • 我需要字典中的不同对象。解决方案是将两者都声明为对象并在必要时删除 FormUrlEncodedContent。由于您的答案涵盖了其中的一部分,因此它被接受为答案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-28
  • 1970-01-01
  • 2015-10-21
  • 2016-12-16
  • 2016-10-24
  • 1970-01-01
相关资源
最近更新 更多