HttpClient postasync,带有自定义标头和应用程序/json,用于正文C#
您好,我想从其HttpClient postasync,带有自定义标头和应用程序/json,用于正文C#,c#,json,httpclient,json-serialization,C#,Json,Httpclient,Json Serialization,您好,我想从其api运行推送应用程序中心。但我不知道如何制作正确的格式 我想从此api中执行postasync: 它需要的标题是: X-API-Token=“{API-Token}”和Content Type=“application/json” 对于正文(内容),我想说: { "notification_content" : { "name" : "Campaign Name", "title" : "Expired Warning",
api
运行推送应用程序中心。但我不知道如何制作正确的格式
我想从此api中执行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);
};
网上有很多文章,例如:非常感谢,先生:)这篇文章真的很有用。我连续三天都在做这件事。。谢谢