c# - HttpClient postasync 与正文 C# 的自定义 header 和 application/json

标签 c# json httpclient json-serialization

您好,我想从其 api 运行推送应用中心。但我不知道如何制作正确的格式。

我想从这个 api 中postasync:https://appcenter.ms/api/v0.1/apps/KacangIjo/ShopDiaryApp/push/notifications

Headers 需要的是: X-API-Token ="{api token }"和 Content Type="application/json"

对于正文(内容)我想放这个:

{
    "notification_content" : {
        "name" : "Campaign Name",
        "title" : "Expired Warning",
        "body" : "You have items that almost expired"
    }
}

我很难以正确的格式编写 HttpClient。 我试过这个但没有工作..

Content = new Content
{
   Name = "Campaign Name",
   Title = "Expired Warning",
   Body = "You have items that almost expired"
};
using (var client = new HttpClient { Timeout = TimeSpan.FromSeconds(30) })
{
   var myContent = JsonConvert.SerializeObject(data);
   client.DefaultRequestHeaders.Add("X-API-Token", "{my api token}");
   client.DefaultRequestHeaders.Accept.Add(new 
   MediaTypeWithQualityHeaderValue("application/json"));
   var builder = new UriBuilder(new Uri("https://appcenter.ms/api/v0.1/apps/KacangIjo/ShopDiaryApp/push/notifications"));
   HttpResponseMessage response = await client.PostAsync(builder.Uri, content);
};

但我知道这段代码:

 {
        "notification_content" : {
            "name" : "Campaign Name",
            "title" : "Expired Warning",
            "body" : "You have items that almost expired"
        }
    }

与此不同的是转换成json格式:

Content = new Content
{
    Name = "Campaign Name",
    Title = "Expired Warning",
    Body = "You have items that almost expired"
};

可以帮助我正确的序列化 Json 格式吗?以及 httpclient header 和正文的正确格式? 我已经找到了很多样本​​,但仍然不知道我想要的样本。 非常感谢你们的帮助:)

最佳答案

您需要构建类似于您所需的 JSON 的对象。

像下面这样创建类。

public class NotificationContent
{
    [JsonProperty("name")]
    public string Name { get; set; }

    [JsonProperty("title")]
    public string Title { get; set; }

    [JsonProperty("body")]
    public string Body { get; set; }
}

public class PostObject
{
    [JsonProperty("notification_content")]
    public NotificationContent NotificationContent { get; set; }
}

上面是正确的结构,现在当你调用JsonConvert.SerializeObject时,你的json将是

 {
    "notification_content" : {
        "name" : "Campaign Name",
        "title" : "Expired Warning",
        "body" : "You have items that almost expired"
    }
} 

下面是http调用的代码

using (var client = new HttpClient { Timeout = TimeSpan.FromSeconds(30) })
    {
        PostObject postObject = new PostObject
        {
            NotificationContent = new NotificationContent
            {
                Name = "Campaign Name",
                Title = "Expired Warning",
                Body = "You have items that almost expired"
            }
        };

        var myContent = JsonConvert.SerializeObject(postObject);
        client.DefaultRequestHeaders.Add("X-API-Token", "{my api token}");
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

        var builder = new UriBuilder(new Uri("https://appcenter.ms/api/v0.1/apps/KacangIjo/ShopDiaryApp/push/notifications"));

        HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, builder.Uri);
        request.Content = new StringContent(myContent, Encoding.UTF8, "application/json");//CONTENT-TYPE header

        HttpResponseMessage response = await client.SendAsync(request);
    };

关于c# - HttpClient postasync 与正文 C# 的自定义 header 和 application/json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53430418/

相关文章:

download - 使用 Google Sign-in 登录网络服务器

c# - 剑道下拉返回对象对象

c# - EDI 文件到管道分隔的平面文件

JAVA:http post 请求

javascript - 使用 javascript/jQuery 构建 JSON 字符串

java - 如何在请求正文中将可序列化对象的映射作为 JSON 传递

c# - HttpResponseMessage 的处理是调用请求流的处理

c# - 表达式树的问题

c# - Xamarin iOS RegisteredForRemoteNotifications 间歇性不调用 Android 100% 的时间工作

javascript - 对象数组的 Angular JSON 过滤器