Branch.io API C# 中的实现

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 您要查找的内容的响应。``