using Newtonsoft.Json; using System; using System.Net.Http; using System.Text; using System.Threading.Tasks; public static async Task<string> tinyccShortenerAsync(string tinyccAPIKey, string LongURL) { string ShortURL; var payload = new { long_url = LongURL, }; using (var httpClient = new HttpClient { BaseAddress = new Uri("https://tiny.cc/tiny/api/3/") }) { httpClient.DefaultRequestHeaders.Add("apikey", tinyccAPIKey); var body = new StringContent(JsonConvert.SerializeObject(payload), UnicodeEncoding.UTF8, "application/json"); using (var response = await httpClient.PostAsync("urls/", body)) { response.EnsureSuccessStatusCode(); var link = JsonConvert.DeserializeObject<dynamic>(await response.Content.ReadAsStringAsync()); ShortURL = "https://" + link.short_url; } } return ShortURL; }
static async Task Main(string[] args) { var task = await tinyccShortenerAsync("TEST_API","http://www.google.com/"); Console.WriteLine(task); Console.ReadLine(); }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)