簡體   English   中英

具有自定義標頭和主體C#的application / json的HttpClient postasync

[英]HttpClient postasync with custom header and application/json for body C#

您好,我想從其api運行push app center。 但是我不知道如何制作正確的格式。

我想從此api進行postasynchttps : postasync

標頭所需的是: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標頭和正文的正確格式? 我已經找到了很多樣本​​,但是仍然對我想要的樣本一無所知。 非常感謝您的幫助:)

您需要構造類似於所需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);
    };

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM