[英]Branch.io API implementation in C#
我想在我的应用程序中实现 Branch.io API。 服务器端是 .Net,我在 C# 中找不到任何 API 实现的解决方案。 Branch.io 中用于使用 API 的每个文档都指的是 Postman 的响应,而 C# 则没有!
有什么办法或者它只是在客户端实现?
这不是最好看的代码,但适用于您的任务:
public class Rootobject
{
public string branch_key { get; set; }
public string channel { get; set; }
public string feature { get; set; }
public string campaign { get; set; }
public string stage { get; set; }
public string[] tags { get; set; }
public Data data { get; set; }
}
public class Data
{
public string canonical_identifier { get; set; }
public string og_title { get; set; }
public string og_description { get; set; }
public string og_image_url { get; set; }
public string desktop_url { get; set; }
public bool custom_boolean { get; set; }
public int custom_integer { get; set; }
public string custom_string { get; set; }
public int[] custom_array { get; set; }
public Custom_Object custom_object { get; set; }
}
public class Custom_Object
{
public string random { get; set; }
}
上面的这些类是要发送的数据。 请求方法应该是这样的:
public async Task GetUrlData(string url, string branchKey)
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri("https://api.branch.io");
client.DefaultRequestHeaders
.Accept
.Add(new MediaTypeWithQualityHeaderValue("application/json"));
string requestUrl = $"/v1/url?url{url}&key={branchKey}";
var payload = new Rootobject();
var stringPayload = JsonConvert.SerializeObject(payload);
var httpContent = new StringContent(stringPayload, Encoding.UTF8, "application/json");
var response = await client.PostAsync(requestUrl, httpContent);
if (response.Content != null)
{
var responseContent = await response.Content.ReadAsStringAsync();
}
}
responseContent 变量是你正在寻找的 api 的响应。``
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.