Реализация API Branch.io в C #

1
задан Mreza Salehim 18 January 2019 в 08:00
поделиться

1 ответ

Это не самый красивый код, но он подходит для вашей задачи:

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 на то, что вы ищете`.

0
ответ дан Ozan Topal 18 January 2019 в 08:00
поделиться
Другие вопросы по тегам:

Похожие вопросы: