private async Task<Uri> ShortenByBitlyAsync(Uri srcUri, string domain = "bit.ly")
{
// 明らかに長くなると推測できる場合は短縮しない
if ("http://bit.ly/xxxx".Length > srcUri.OriginalString.Length)
return srcUri;
// bit.ly 短縮機能実装のプライバシー問題の暫定対応
// ログインIDとAPIキーが指定されていない場合は短縮せずにPOSTする
// 参照: http://sourceforge.jp/projects/opentween/lists/archive/dev/2012-January/000020.html
if (string.IsNullOrEmpty(this.BitlyId) || string.IsNullOrEmpty(this.BitlyKey))
return srcUri;
var query = new Dictionary<string, string>
{
["login"] = this.BitlyId,
["apiKey"] = this.BitlyKey,
["format"] = "txt",
["domain"] = domain,
["longUrl"] = srcUri.OriginalString,
};
var uri = new Uri("https://api-ssl.bitly.com/v3/shorten?" + MyCommon.BuildQueryString(query));
using (var response = await this.http.GetAsync(uri).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());
}
}