private async Task<Uri> ShortenByTinyUrlAsync(Uri srcUri)
{
// 明らかに長くなると推測できる場合は短縮しない
if ("http://tinyurl.com/xxxxxx".Length > srcUri.OriginalString.Length)
return srcUri;
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("url", srcUri.OriginalString),
});
using (var response = await this.http.PostAsync("http://tinyurl.com/api-create.php", content).ConfigureAwait(false))
{
response.EnsureSuccessStatusCode();
var result = await response.Content.ReadAsStringAsync()
.ConfigureAwait(false);
if (!Regex.IsMatch(result, @"^https?://"))
throw new WebApiException("Failed to create URL.", result);
return new Uri(result.TrimEnd());
}
}