HttpClient postasync,带有自定义标头和应用程序/json,用于正文 C#

Posted

技术标签:

【中文标题】HttpClient postasync,带有自定义标头和应用程序/json,用于正文 C#【英文标题】:HttpClient postasync with custom header and application/json for body C# 【发布时间】:2018-11-22 11:51:21 【问题描述】:

您好,我想从其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 标头和正文的正确格式? 我已经找到了很多样品,但仍然不知道我想要的那个。 非常感谢你们的帮助:)

【问题讨论】:

网上有很多文章,例如stevejgordon.co.uk/… 【参考方案1】:

您需要按照您所需的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);
    ;

【讨论】:

非常感谢先生 :) 这个真的有效。我连续工作了 3 天。谢谢

以上是关于HttpClient postasync,带有自定义标头和应用程序/json,用于正文 C#的主要内容,如果未能解决你的问题,请参考以下文章

带有json字符串的后台任务中的UWP Httpclient postasync

HttpClient 中的 PostAsync 不会将数据发送到我的 webapi

HttpClient.PostAsync 崩溃应用。如何捕捉异常?

异步等待 HttpClient.PostAsync 调用

查看 HttpClient.PostAsync 的响应正文

如何使用 HttpClient.PostAsync 发送 XML 内容?