| 1 |
/*-------------------------------------------------------------------------- |
| 2 |
* URL短縮支援ライブラリ |
| 3 |
* version 1.0.5 (Dec. 29th, 2010) |
| 4 |
* |
| 5 |
* This code is written by aqua877 http://aqua-s.ddo.jp/ |
| 6 |
*--------------------------------------------------------------------------*/ |
| 7 |
using System; |
| 8 |
using System.Collections.Generic; |
| 9 |
using System.Linq; |
| 10 |
using System.Text; |
| 11 |
using System.Net; |
| 12 |
using System.ComponentModel; |
| 13 |
using System.Xml.Linq; |
| 14 |
using System.Text.RegularExpressions; |
| 15 |
using System.Reflection; |
| 16 |
using System.Collections.Specialized; |
| 17 |
|
| 18 |
namespace Aqua877.WinApp.Lib |
| 19 |
{ |
| 20 |
/// <summary> |
| 21 |
/// ShortenURLFinishedイベントのデータを提供します。 |
| 22 |
/// </summary> |
| 23 |
public class ShortenURLFinishedEventArgs : EventArgs |
| 24 |
{ |
| 25 |
public Uri URLBeforeShortening |
| 26 |
{ |
| 27 |
get; |
| 28 |
private set; |
| 29 |
} |
| 30 |
|
| 31 |
public Uri URLAfterShortening |
| 32 |
{ |
| 33 |
get; |
| 34 |
private set; |
| 35 |
} |
| 36 |
|
| 37 |
public Exception Error |
| 38 |
{ |
| 39 |
get; |
| 40 |
private set; |
| 41 |
} |
| 42 |
|
| 43 |
public ShortenURLFinishedEventArgs(Uri urlBeforeShortening, Uri urlAfterShortening, Exception error) |
| 44 |
: base() |
| 45 |
{ |
| 46 |
this.URLBeforeShortening = urlBeforeShortening; |
| 47 |
this.URLAfterShortening = urlAfterShortening; |
| 48 |
this.Error = error; |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
/// <summary> |
| 53 |
/// ExpandURLFinishedイベントのデータを提供します。 |
| 54 |
/// </summary> |
| 55 |
public class ExpandURLFinishedEventArgs : EventArgs |
| 56 |
{ |
| 57 |
public Uri URLBeforeExpanding |
| 58 |
{ |
| 59 |
get; |
| 60 |
private set; |
| 61 |
} |
| 62 |
|
| 63 |
public Uri URLAfterExpanding |
| 64 |
{ |
| 65 |
get; |
| 66 |
private set; |
| 67 |
} |
| 68 |
|
| 69 |
public Exception Error |
| 70 |
{ |
| 71 |
get; |
| 72 |
private set; |
| 73 |
} |
| 74 |
|
| 75 |
public ExpandURLFinishedEventArgs(Uri urlBeforeExpanding, Uri urlAfterExpanding, Exception error) |
| 76 |
: base() |
| 77 |
{ |
| 78 |
this.URLBeforeExpanding = urlBeforeExpanding; |
| 79 |
this.URLAfterExpanding = urlAfterExpanding; |
| 80 |
this.Error = error; |
| 81 |
} |
| 82 |
} |
| 83 |
|
| 84 |
/// <summary> |
| 85 |
/// IURLShortenのShortenURLFinishedイベントを処理するメソッドを表します。 |
| 86 |
/// </summary> |
| 87 |
/// <param name="sender"></param> |
| 88 |
/// <param name="e"></param> |
| 89 |
public delegate void ShortenURLFinishedEventHandler(object sender, ShortenURLFinishedEventArgs e); |
| 90 |
|
| 91 |
/// <summary> |
| 92 |
/// IURLShortenのExpandURLFinishedイベントを処理するメソッドを表します。 |
| 93 |
/// </summary> |
| 94 |
/// <param name="sender"></param> |
| 95 |
/// <param name="e"></param> |
| 96 |
public delegate void ExpandURLFinishedEventHandler(object sender, ExpandURLFinishedEventArgs e); |
| 97 |
|
| 98 |
/// <summary> |
| 99 |
/// URLの短縮サービスを利用するための共通のメソッドを定義します。 |
| 100 |
/// </summary> |
| 101 |
public interface IURLShorten : IDisposable |
| 102 |
{ |
| 103 |
/// <summary> |
| 104 |
/// 指定したURLを短縮します。 |
| 105 |
/// </summary> |
| 106 |
/// <param name="url"></param> |
| 107 |
/// <returns></returns> |
| 108 |
Uri ShortenURL(Uri url); |
| 109 |
|
| 110 |
/// <summary> |
| 111 |
/// 指定した短縮URLを復元します。 |
| 112 |
/// </summary> |
| 113 |
/// <param name="url"></param> |
| 114 |
/// <returns></returns> |
| 115 |
Uri ExpandURL(Uri url); |
| 116 |
|
| 117 |
/// <summary> |
| 118 |
/// 指定したURLを短縮します。このメソッドは、呼び出し元のスレッドをブロックしません。 |
| 119 |
/// </summary> |
| 120 |
/// <param name="url"></param> |
| 121 |
void BeginShortenURL(Uri url); |
| 122 |
|
| 123 |
/// <summary> |
| 124 |
/// 保留中の非同期URL短縮操作を終了します。 |
| 125 |
/// </summary> |
| 126 |
void EndShortenURL(); |
| 127 |
|
| 128 |
/// <summary> |
| 129 |
/// 指定した短縮URLを復元します。このメソッドは、呼び出し元のスレッドをブロックしません。 |
| 130 |
/// </summary> |
| 131 |
/// <param name="url"></param> |
| 132 |
void BeginExpandURL(Uri url); |
| 133 |
|
| 134 |
/// <summary> |
| 135 |
/// 保留中の非同期短縮URL復元操作を終了します。 |
| 136 |
/// </summary> |
| 137 |
void EndExpandURL(); |
| 138 |
|
| 139 |
/// <summary> |
| 140 |
/// 非同期のURL短縮操作の完了時に発生します。 |
| 141 |
/// </summary> |
| 142 |
event ShortenURLFinishedEventHandler ShortenURLFinished; |
| 143 |
|
| 144 |
/// <summary> |
| 145 |
/// 非同期の短縮URL復元操作完了時に発生します。 |
| 146 |
/// </summary> |
| 147 |
event ExpandURLFinishedEventHandler ExpandURLFinished; |
| 148 |
} |
| 149 |
|
| 150 |
/// <summary> |
| 151 |
/// bit.lyでのURL短縮用のメソッドを提供します。 |
| 152 |
/// </summary> |
| 153 |
public sealed class BitLyURLShortener : IURLShorten |
| 154 |
{ |
| 155 |
private WebClient ShortenServiceConnector; |
| 156 |
private WebClient ExpandServiceConnector; |
| 157 |
|
| 158 |
private string User; |
| 159 |
private string ApiKey; |
| 160 |
|
| 161 |
public BitLyURLShortener(string user, string apiKey) |
| 162 |
{ |
| 163 |
if (user == null) |
| 164 |
{ |
| 165 |
throw new ArgumentNullException("user"); |
| 166 |
} |
| 167 |
|
| 168 |
if (apiKey == null) |
| 169 |
{ |
| 170 |
throw new ArgumentNullException("apiKey"); |
| 171 |
} |
| 172 |
|
| 173 |
if (user == string.Empty) |
| 174 |
{ |
| 175 |
throw new ArgumentException("ユーザー名を空にすることは出来ません。", "user"); |
| 176 |
} |
| 177 |
|
| 178 |
if (apiKey == string.Empty) |
| 179 |
{ |
| 180 |
throw new ArgumentException("APIキーを空にすることは出来ません。", "apiKey"); |
| 181 |
} |
| 182 |
|
| 183 |
this.User = user; |
| 184 |
this.ApiKey = apiKey; |
| 185 |
} |
| 186 |
|
| 187 |
private void OnShortenURLFinished(ShortenURLFinishedEventArgs e) |
| 188 |
{ |
| 189 |
if (this.ShortenURLFinished != null) |
| 190 |
{ |
| 191 |
this.ShortenURLFinished(this, e); |
| 192 |
} |
| 193 |
} |
| 194 |
|
| 195 |
private void OnExpandURLFinished(ExpandURLFinishedEventArgs e) |
| 196 |
{ |
| 197 |
if (this.ExpandURLFinished != null) |
| 198 |
{ |
| 199 |
this.ExpandURLFinished(this, e); |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
public Uri ShortenURL(Uri url) |
| 204 |
{ |
| 205 |
if (url == null) |
| 206 |
{ |
| 207 |
throw new ArgumentNullException("url"); |
| 208 |
} |
| 209 |
|
| 210 |
this.ShortenServiceConnector = new WebClient() |
| 211 |
{ |
| 212 |
Encoding = Encoding.UTF8 |
| 213 |
}; |
| 214 |
|
| 215 |
string api = String.Format("http://api.bit.ly/shorten?version=2.0.1&format=xml&login={0}&apiKey={1}&longUrl={2}", this.User, this.ApiKey, url); |
| 216 |
|
| 217 |
string result = this.ShortenServiceConnector.DownloadString(api); |
| 218 |
|
| 219 |
XDocument document = XDocument.Parse(result); |
| 220 |
|
| 221 |
string errorCode = document.Descendants("errorCode").Single().Value; |
| 222 |
string errorMessgage = document.Descendants("errorMessage").Single().Value; |
| 223 |
|
| 224 |
if (errorCode != "0" && errorMessgage != "") |
| 225 |
{ |
| 226 |
throw new WebException(String.Format("bit.ly APIがエラーを返しました。エラーコード : {0} エラーメッセージ : {1}", errorCode, errorMessgage)); |
| 227 |
} |
| 228 |
|
| 229 |
string shortenURL = document.Descendants("shortUrl").Single().Value; |
| 230 |
return new Uri(shortenURL); |
| 231 |
} |
| 232 |
|
| 233 |
public Uri ExpandURL(Uri url) |
| 234 |
{ |
| 235 |
if (url == null) |
| 236 |
{ |
| 237 |
throw new ArgumentNullException("url"); |
| 238 |
} |
| 239 |
|
| 240 |
if (url.Host != "bit.ly") |
| 241 |
{ |
| 242 |
throw new ArgumentException("bit.lyによって短縮されたURLではありません。", "url"); |
| 243 |
} |
| 244 |
|
| 245 |
this.ExpandServiceConnector = new WebClient() |
| 246 |
{ |
| 247 |
Encoding = Encoding.UTF8 |
| 248 |
}; |
| 249 |
|
| 250 |
string api = String.Format("{0}+", url); |
| 251 |
|
| 252 |
string result = this.ShortenServiceConnector.DownloadString(api); |
| 253 |
|
| 254 |
Regex pattern = new Regex(String.Format("<dd><a href=\"(.*?)\" onclick=\"javascript:document.location.href='{0}';\">", url)); |
| 255 |
Match match = pattern.Match(result); |
| 256 |
|
| 257 |
if (match.Success) |
| 258 |
{ |
| 259 |
return new Uri(match.Groups[1].Value); |
| 260 |
} |
| 261 |
else |
| 262 |
{ |
| 263 |
return null; |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
public void BeginShortenURL(Uri url) |
| 268 |
{ |
| 269 |
if (url == null) |
| 270 |
{ |
| 271 |
throw new ArgumentNullException("url"); |
| 272 |
} |
| 273 |
|
| 274 |
this.ShortenServiceConnector = new WebClient() |
| 275 |
{ |
| 276 |
Encoding = Encoding.UTF8 |
| 277 |
}; |
| 278 |
|
| 279 |
string api = String.Format("http://api.bit.ly/shorten?version=2.0.1&format=xml&login={0}&apiKey={1}&longUrl={2}", this.User, this.ApiKey, url); |
| 280 |
|
| 281 |
this.ShortenServiceConnector.DownloadStringCompleted += |
| 282 |
(sender, e) => |
| 283 |
{ |
| 284 |
if (e.Error != null) |
| 285 |
{ |
| 286 |
this.OnShortenURLFinished(new ShortenURLFinishedEventArgs(url, null, e.Error)); |
| 287 |
} |
| 288 |
else |
| 289 |
{ |
| 290 |
XDocument document = XDocument.Parse(e.Result); |
| 291 |
|
| 292 |
string errorCode = document.Descendants("errorCode").Single().Value; |
| 293 |
string errorMessgage = document.Descendants("errorMessage").Single().Value; |
| 294 |
|
| 295 |
if (errorCode != "0" && errorMessgage != "") |
| 296 |
{ |
| 297 |
Exception error = new WebException(String.Format("bit.ly APIがエラーを返しました。エラーコード : {0} エラーメッセージ : {1}", errorCode, errorMessgage)); |
| 298 |
this.OnShortenURLFinished(new ShortenURLFinishedEventArgs(url, null, error)); |
| 299 |
} |
| 300 |
else |
| 301 |
{ |
| 302 |
string shortenURL = document.Descendants("shortUrl").Single().Value; |
| 303 |
|
| 304 |
this.OnShortenURLFinished(new ShortenURLFinishedEventArgs(url, new Uri(shortenURL), null)); |
| 305 |
} |
| 306 |
} |
| 307 |
}; |
| 308 |
|
| 309 |
this.ShortenServiceConnector.DownloadStringAsync(new Uri(api)); |
| 310 |
} |
| 311 |
|
| 312 |
public void EndShortenURL() |
| 313 |
{ |
| 314 |
this.ShortenServiceConnector.CancelAsync(); |
| 315 |
} |
| 316 |
|
| 317 |
public void BeginExpandURL(Uri url) |
| 318 |
{ |
| 319 |
if (url == null) |
| 320 |
{ |
| 321 |
throw new ArgumentNullException("url"); |
| 322 |
} |
| 323 |
|
| 324 |
if (url.Host != "bit.ly") |
| 325 |
{ |
| 326 |
throw new ArgumentException("bit.lyによって短縮されたURLではありません。", "url"); |
| 327 |
} |
| 328 |
|
| 329 |
this.ExpandServiceConnector = new WebClient() |
| 330 |
{ |
| 331 |
Encoding = Encoding.UTF8 |
| 332 |
}; |
| 333 |
|
| 334 |
string api = String.Format("{0}+", url); |
| 335 |
|
| 336 |
this.ExpandServiceConnector.DownloadStringCompleted += |
| 337 |
(sender, e) => |
| 338 |
{ |
| 339 |
if (e.Error != null) |
| 340 |
{ |
| 341 |
this.OnExpandURLFinished(new ExpandURLFinishedEventArgs(url, null, e.Error)); |
| 342 |
} |
| 343 |
else |
| 344 |
{ |
| 345 |
Regex pattern = new Regex(String.Format("<dd><a href=\"(.*?)\" onclick=\"javascript:document.location.href='{0}';\">", url)); |
| 346 |
Match match = pattern.Match(e.Result); |
| 347 |
|
| 348 |
if (match.Success) |
| 349 |
{ |
| 350 |
this.OnExpandURLFinished(new ExpandURLFinishedEventArgs(url, new Uri(match.Groups[1].Value), null)); |
| 351 |
} |
| 352 |
else |
| 353 |
{ |
| 354 |
this.OnExpandURLFinished(new ExpandURLFinishedEventArgs(url, null, null)); |
| 355 |
} |
| 356 |
} |
| 357 |
}; |
| 358 |
|
| 359 |
this.ExpandServiceConnector.DownloadStringAsync(new Uri(api)); |
| 360 |
} |
| 361 |
|
| 362 |
public void EndExpandURL() |
| 363 |
{ |
| 364 |
this.ExpandServiceConnector.CancelAsync(); |
| 365 |
} |
| 366 |
|
| 367 |
public void Dispose() |
| 368 |
{ |
| 369 |
this.ShortenServiceConnector.Dispose(); |
| 370 |
this.ExpandServiceConnector.Dispose(); |
| 371 |
} |
| 372 |
|
| 373 |
public event ShortenURLFinishedEventHandler ShortenURLFinished; |
| 374 |
|
| 375 |
public event ExpandURLFinishedEventHandler ExpandURLFinished; |
| 376 |
} |
| 377 |
|
| 378 |
/// <summary> |
| 379 |
/// ux.nuでのURL短縮用のメソッドを提供します。 |
| 380 |
/// </summary> |
| 381 |
public sealed class UxNuURLShortener : IURLShorten |
| 382 |
{ |
| 383 |
private WebClient ShortenServiceConnector; |
| 384 |
private WebClient ExpandServiceConnector; |
| 385 |
|
| 386 |
public UxNuURLShortener() |
| 387 |
{ |
| 388 |
} |
| 389 |
|
| 390 |
private void OnShortenURLFinished(ShortenURLFinishedEventArgs e) |
| 391 |
{ |
| 392 |
if (this.ShortenURLFinished != null) |
| 393 |
{ |
| 394 |
this.ShortenURLFinished(this, e); |
| 395 |
} |
| 396 |
} |
| 397 |
|
| 398 |
private void OnExpandURLFinished(ExpandURLFinishedEventArgs e) |
| 399 |
{ |
| 400 |
if (this.ExpandURLFinished != null) |
| 401 |
{ |
| 402 |
this.ExpandURLFinished(this, e); |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
public Uri ShortenURL(Uri url) |
| 407 |
{ |
| 408 |
if (url == null) |
| 409 |
{ |
| 410 |
throw new ArgumentNullException("url"); |
| 411 |
} |
| 412 |
|
| 413 |
this.ShortenServiceConnector = new WebClient() |
| 414 |
{ |
| 415 |
Encoding = Encoding.UTF8 |
| 416 |
}; |
| 417 |
|
| 418 |
string api = String.Format("http://ux.nu/api/short?url={0}&format=plain", url); |
| 419 |
|
| 420 |
string result = this.ShortenServiceConnector.DownloadString(api); |
| 421 |
|
| 422 |
return new Uri(result); |
| 423 |
} |
| 424 |
|
| 425 |
public Uri ExpandURL(Uri url) |
| 426 |
{ |
| 427 |
if (url == null) |
| 428 |
{ |
| 429 |
throw new ArgumentNullException("url"); |
| 430 |
} |
| 431 |
|
| 432 |
if (url.Host != "ux.nu") |
| 433 |
{ |
| 434 |
throw new ArgumentException("ux.nuによって短縮されたURLではありません。", "url"); |
| 435 |
} |
| 436 |
|
| 437 |
this.ExpandServiceConnector = new WebClient() |
| 438 |
{ |
| 439 |
Encoding = Encoding.UTF8 |
| 440 |
}; |
| 441 |
|
| 442 |
string api = String.Format("http://ux.nu/hugeurl?url={0}", url); |
| 443 |
|
| 444 |
string result = this.ExpandServiceConnector.DownloadString(api); |
| 445 |
|
| 446 |
return new Uri(result); |
| 447 |
} |
| 448 |
|
| 449 |
public void BeginShortenURL(Uri url) |
| 450 |
{ |
| 451 |
if (url == null) |
| 452 |
{ |
| 453 |
throw new ArgumentNullException("url"); |
| 454 |
} |
| 455 |
|
| 456 |
this.ShortenServiceConnector = new WebClient() |
| 457 |
{ |
| 458 |
Encoding = Encoding.UTF8 |
| 459 |
}; |
| 460 |
|
| 461 |
string api = String.Format("http://ux.nu/api/short?url={0}&format=plain", url); |
| 462 |
|
| 463 |
this.ShortenServiceConnector.DownloadStringCompleted += |
| 464 |
(sender, e) => |
| 465 |
{ |
| 466 |
if (e.Error != null) |
| 467 |
{ |
| 468 |
this.OnShortenURLFinished(new ShortenURLFinishedEventArgs(url, null, e.Error)); |
| 469 |
} |
| 470 |
else |
| 471 |
{ |
| 472 |
this.OnShortenURLFinished(new ShortenURLFinishedEventArgs(url, new Uri(e.Result), null)); |
| 473 |
} |
| 474 |
}; |
| 475 |
|
| 476 |
this.ShortenServiceConnector.DownloadStringAsync(new Uri(api)); |
| 477 |
} |
| 478 |
|
| 479 |
public void EndShortenURL() |
| 480 |
{ |
| 481 |
this.ShortenServiceConnector.CancelAsync(); |
| 482 |
} |
| 483 |
|
| 484 |
public void BeginExpandURL(Uri url) |
| 485 |
{ |
| 486 |
if (url == null) |
| 487 |
{ |
| 488 |
throw new ArgumentNullException("url"); |
| 489 |
} |
| 490 |
|
| 491 |
this.ExpandServiceConnector = new WebClient() |
| 492 |
{ |
| 493 |
Encoding = Encoding.UTF8 |
| 494 |
}; |
| 495 |
|
| 496 |
if (url.Host != "ux.nu") |
| 497 |
{ |
| 498 |
throw new ArgumentException("ux.nuによって短縮されたURLではありません。", "url"); |
| 499 |
} |
| 500 |
|
| 501 |
string api = String.Format("http://ux.nu/hugeurl?url={0}", url); |
| 502 |
|
| 503 |
this.ExpandServiceConnector.DownloadStringCompleted += |
| 504 |
(sender, e) => |
| 505 |
{ |
| 506 |
if (e.Error != null) |
| 507 |
{ |
| 508 |
this.OnExpandURLFinished(new ExpandURLFinishedEventArgs(url, null, e.Error)); |
| 509 |
} |
| 510 |
else |
| 511 |
{ |
| 512 |
this.OnExpandURLFinished(new ExpandURLFinishedEventArgs(url, new Uri(e.Result), null)); |
| 513 |
} |
| 514 |
}; |
| 515 |
|
| 516 |
this.ExpandServiceConnector.DownloadStringAsync(new Uri(api)); |
| 517 |
} |
| 518 |
|
| 519 |
public void EndExpandURL() |
| 520 |
{ |
| 521 |
this.ExpandServiceConnector.CancelAsync(); |
| 522 |
} |
| 523 |
|
| 524 |
public void Dispose() |
| 525 |
{ |
| 526 |
this.ShortenServiceConnector.Dispose(); |
| 527 |
this.ExpandServiceConnector.Dispose(); |
| 528 |
} |
| 529 |
|
| 530 |
public event ShortenURLFinishedEventHandler ShortenURLFinished; |
| 531 |
|
| 532 |
public event ExpandURLFinishedEventHandler ExpandURLFinished; |
| 533 |
} |
| 534 |
|
| 535 |
/// <summary> |
| 536 |
/// j.mpでのURL短縮用のメソッドを提供します。 |
| 537 |
/// </summary> |
| 538 |
public sealed class JMpURLShortener : IURLShorten |
| 539 |
{ |
| 540 |
private WebClient ShortenServiceConnector; |
| 541 |
private WebClient ExpandServiceConnector; |
| 542 |
|
| 543 |
private string User; |
| 544 |
private string ApiKey; |
| 545 |
|
| 546 |
public JMpURLShortener(string user, string apiKey) |
| 547 |
{ |
| 548 |
if (user == null) |
| 549 |
{ |
| 550 |
throw new ArgumentNullException("user"); |
| 551 |
} |
| 552 |
|
| 553 |
if (apiKey == null) |
| 554 |
{ |
| 555 |
throw new ArgumentNullException("apiKey"); |
| 556 |
} |
| 557 |
|
| 558 |
if (user == string.Empty) |
| 559 |
{ |
| 560 |
throw new ArgumentException("ユーザー名を空にすることは出来ません。", "user"); |
| 561 |
} |
| 562 |
|
| 563 |
if (apiKey == string.Empty) |
| 564 |
{ |
| 565 |
throw new ArgumentException("APIキーを空にすることは出来ません。", "apiKey"); |
| 566 |
} |
| 567 |
|
| 568 |
this.User = user; |
| 569 |
this.ApiKey = apiKey; |
| 570 |
} |
| 571 |
|
| 572 |
private void OnShortenURLFinished(ShortenURLFinishedEventArgs e) |
| 573 |
{ |
| 574 |
if (this.ShortenURLFinished != null) |
| 575 |
{ |
| 576 |
this.ShortenURLFinished(this, e); |
| 577 |
} |
| 578 |
} |
| 579 |
|
| 580 |
private void OnExpandURLFinished(ExpandURLFinishedEventArgs e) |
| 581 |
{ |
| 582 |
if (this.ExpandURLFinished != null) |
| 583 |
{ |
| 584 |
this.ExpandURLFinished(this, e); |
| 585 |
} |
| 586 |
} |
| 587 |
|
| 588 |
public Uri ShortenURL(Uri url) |
| 589 |
{ |
| 590 |
if (url == null) |
| 591 |
{ |
| 592 |
throw new ArgumentNullException("url"); |
| 593 |
} |
| 594 |
|
| 595 |
this.ShortenServiceConnector = new WebClient() |
| 596 |
{ |
| 597 |
Encoding = Encoding.UTF8 |
| 598 |
}; |
| 599 |
|
| 600 |
string api = String.Format("http://api.j.mp/shorten?version=2.0.1&format=xml&login={0}&apiKey={1}&longUrl={2}", this.User, this.ApiKey, url); |
| 601 |
|
| 602 |
string result = this.ShortenServiceConnector.DownloadString(api); |
| 603 |
|
| 604 |
XDocument document = XDocument.Parse(result); |
| 605 |
|
| 606 |
string errorCode = document.Descendants("errorCode").Single().Value; |
| 607 |
string errorMessgage = document.Descendants("errorMessage").Single().Value; |
| 608 |
|
| 609 |
if (errorCode != "0" && errorMessgage != "") |
| 610 |
{ |
| 611 |
throw new WebException(String.Format("j.mp APIがエラーを返しました。エラーコード : {0} エラーメッセージ : {1}", errorCode, errorMessgage)); |
| 612 |
} |
| 613 |
|
| 614 |
string shortenURL = document.Descendants("shortUrl").Single().Value; |
| 615 |
return new Uri(shortenURL); |
| 616 |
} |
| 617 |
|
| 618 |
public Uri ExpandURL(Uri url) |
| 619 |
{ |
| 620 |
if (url == null) |
| 621 |
{ |
| 622 |
throw new ArgumentNullException("url"); |
| 623 |
} |
| 624 |
|
| 625 |
if (url.Host != "j.mp") |
| 626 |
{ |
| 627 |
throw new ArgumentException("j.mpによって短縮されたURLではありません。", "url"); |
| 628 |
} |
| 629 |
|
| 630 |
this.ExpandServiceConnector = new WebClient() |
| 631 |
{ |
| 632 |
Encoding = Encoding.UTF8 |
| 633 |
}; |
| 634 |
|
| 635 |
string api = String.Format("{0}+", url); |
| 636 |
|
| 637 |
string result = this.ShortenServiceConnector.DownloadString(api); |
| 638 |
|
| 639 |
Regex pattern = new Regex(String.Format("<dd><a href=\"(.*?)\" onclick=\"javascript:document.location.href='{0}';\">", url)); |
| 640 |
Match match = pattern.Match(result); |
| 641 |
|
| 642 |
if (match.Success) |
| 643 |
{ |
| 644 |
return new Uri(match.Groups[1].Value); |
| 645 |
} |
| 646 |
else |
| 647 |
{ |
| 648 |
return null; |
| 649 |
} |
| 650 |
} |
| 651 |
|
| 652 |
public void BeginShortenURL(Uri url) |
| 653 |
{ |
| 654 |
if (url == null) |
| 655 |
{ |
| 656 |
throw new ArgumentNullException("url"); |
| 657 |
} |
| 658 |
|
| 659 |
this.ShortenServiceConnector = new WebClient() |
| 660 |
{ |
| 661 |
Encoding = Encoding.UTF8 |
| 662 |
}; |
| 663 |
|
| 664 |
string api = String.Format("http://api.j.mp/shorten?version=2.0.1&format=xml&login={0}&apiKey={1}&longUrl={2}", this.User, this.ApiKey, url); |
| 665 |
|
| 666 |
this.ShortenServiceConnector.DownloadStringCompleted += |
| 667 |
(sender, e) => |
| 668 |
{ |
| 669 |
if (e.Error != null) |
| 670 |
{ |
| 671 |
this.OnShortenURLFinished(new ShortenURLFinishedEventArgs(url, null, e.Error)); |
| 672 |
} |
| 673 |
else |
| 674 |
{ |
| 675 |
XDocument document = XDocument.Parse(e.Result); |
| 676 |
|
| 677 |
string errorCode = document.Descendants("errorCode").Single().Value; |
| 678 |
string errorMessgage = document.Descendants("errorMessage").Single().Value; |
| 679 |
|
| 680 |
if (errorCode != "0" && errorMessgage != "") |
| 681 |
{ |
| 682 |
Exception error = new WebException(String.Format("j.mp APIがエラーを返しました。エラーコード : {0} エラーメッセージ : {1}", errorCode, errorMessgage)); |
| 683 |
this.OnShortenURLFinished(new ShortenURLFinishedEventArgs(url, null, error)); |
| 684 |
} |
| 685 |
else |
| 686 |
{ |
| 687 |
string shortenURL = document.Descendants("shortUrl").Single().Value; |
| 688 |
|
| 689 |
this.OnShortenURLFinished(new ShortenURLFinishedEventArgs(url, new Uri(shortenURL), null)); |
| 690 |
} |
| 691 |
} |
| 692 |
}; |
| 693 |
|
| 694 |
this.ShortenServiceConnector.DownloadStringAsync(new Uri(api)); |
| 695 |
} |
| 696 |
|
| 697 |
public void EndShortenURL() |
| 698 |
{ |
| 699 |
this.ShortenServiceConnector.CancelAsync(); |
| 700 |
} |
| 701 |
|
| 702 |
public void BeginExpandURL(Uri url) |
| 703 |
{ |
| 704 |
if (url == null) |
| 705 |
{ |
| 706 |
throw new ArgumentNullException("url"); |
| 707 |
} |
| 708 |
|
| 709 |
if (url.Host != "bit.ly") |
| 710 |
{ |
| 711 |
throw new ArgumentException("j.mpによって短縮されたURLではありません。", "url"); |
| 712 |
} |
| 713 |
|
| 714 |
this.ExpandServiceConnector = new WebClient() |
| 715 |
{ |
| 716 |
Encoding = Encoding.UTF8 |
| 717 |
}; |
| 718 |
|
| 719 |
string api = String.Format("{0}+", url); |
| 720 |
|
| 721 |
this.ExpandServiceConnector.DownloadStringCompleted += |
| 722 |
(sender, e) => |
| 723 |
{ |
| 724 |
if (e.Error != null) |
| 725 |
{ |
| 726 |
this.OnExpandURLFinished(new ExpandURLFinishedEventArgs(url, null, e.Error)); |
| 727 |
} |
| 728 |
else |
| 729 |
{ |
| 730 |
Regex pattern = new Regex(String.Format("<dd><a href=\"(.*?)\" onclick=\"javascript:document.location.href='{0}';\">", url)); |
| 731 |
Match match = pattern.Match(e.Result); |
| 732 |
|
| 733 |
if (match.Success) |
| 734 |
{ |
| 735 |
this.OnExpandURLFinished(new ExpandURLFinishedEventArgs(url, new Uri(match.Groups[1].Value), null)); |
| 736 |
} |
| 737 |
else |
| 738 |
{ |
| 739 |
this.OnExpandURLFinished(new ExpandURLFinishedEventArgs(url, null, null)); |
| 740 |
} |
| 741 |
} |
| 742 |
}; |
| 743 |
|
| 744 |
this.ExpandServiceConnector.DownloadStringAsync(new Uri(api)); |
| 745 |
} |
| 746 |
|
| 747 |
public void EndExpandURL() |
| 748 |
{ |
| 749 |
this.ExpandServiceConnector.CancelAsync(); |
| 750 |
} |
| 751 |
|
| 752 |
public void Dispose() |
| 753 |
{ |
| 754 |
this.ShortenServiceConnector.Dispose(); |
| 755 |
this.ExpandServiceConnector.Dispose(); |
| 756 |
} |
| 757 |
|
| 758 |
public event ShortenURLFinishedEventHandler ShortenURLFinished; |
| 759 |
|
| 760 |
public event ExpandURLFinishedEventHandler ExpandURLFinished; |
| 761 |
} |
| 762 |
|
| 763 |
/// <summary> |
| 764 |
/// is.gdでのURL短縮用のメソッドを提供します。 |
| 765 |
/// </summary> |
| 766 |
public sealed class IsGdURLShortener : IURLShorten |
| 767 |
{ |
| 768 |
private WebClient ShortenServiceConnector; |
| 769 |
private WebClient ExpandServiceConnector; |
| 770 |
|
| 771 |
public IsGdURLShortener() |
| 772 |
{ |
| 773 |
} |
| 774 |
|
| 775 |
private void OnShortenURLFinished(ShortenURLFinishedEventArgs e) |
| 776 |
{ |
| 777 |
if (this.ShortenURLFinished != null) |
| 778 |
{ |
| 779 |
this.ShortenURLFinished(this, e); |
| 780 |
} |
| 781 |
} |
| 782 |
|
| 783 |
private void OnExpandURLFinished(ExpandURLFinishedEventArgs e) |
| 784 |
{ |
| 785 |
if (this.ExpandURLFinished != null) |
| 786 |
{ |
| 787 |
this.ExpandURLFinished(this, e); |
| 788 |
} |
| 789 |
} |
| 790 |
|
| 791 |
public Uri ShortenURL(Uri url) |
| 792 |
{ |
| 793 |
if (url == null) |
| 794 |
{ |
| 795 |
throw new ArgumentNullException("url"); |
| 796 |
} |
| 797 |
|
| 798 |
this.ShortenServiceConnector = new WebClient() |
| 799 |
{ |
| 800 |
Encoding = Encoding.UTF8 |
| 801 |
}; |
| 802 |
|
| 803 |
string api = String.Format("http://is.gd/api.php?longurl={0}", url); |
| 804 |
|
| 805 |
string result = this.ShortenServiceConnector.DownloadString(api); |
| 806 |
|
| 807 |
return new Uri(result); |
| 808 |
} |
| 809 |
|
| 810 |
public Uri ExpandURL(Uri url) |
| 811 |
{ |
| 812 |
if (url == null) |
| 813 |
{ |
| 814 |
throw new ArgumentNullException("url"); |
| 815 |
} |
| 816 |
|
| 817 |
if (url.Host != "is.gd") |
| 818 |
{ |
| 819 |
throw new ArgumentException("is.gdによって短縮されたURLではありません。", "url"); |
| 820 |
} |
| 821 |
|
| 822 |
this.ExpandServiceConnector = new WebClient() |
| 823 |
{ |
| 824 |
Encoding = Encoding.UTF8 |
| 825 |
}; |
| 826 |
|
| 827 |
this.ExpandServiceConnector.DownloadString(url); |
| 828 |
|
| 829 |
var info = typeof(WebClient).GetField("m_WebResponse", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance); |
| 830 |
var response = info.GetValue(this.ExpandServiceConnector) as HttpWebResponse; |
| 831 |
|
| 832 |
return response.ResponseUri; |
| 833 |
} |
| 834 |
|
| 835 |
public void BeginShortenURL(Uri url) |
| 836 |
{ |
| 837 |
if (url == null) |
| 838 |
{ |
| 839 |
throw new ArgumentNullException("url"); |
| 840 |
} |
| 841 |
|
| 842 |
this.ShortenServiceConnector = new WebClient() |
| 843 |
{ |
| 844 |
Encoding = Encoding.UTF8 |
| 845 |
}; |
| 846 |
|
| 847 |
string api = String.Format("http://is.gd/api.php?longurl={0}", url); |
| 848 |
|
| 849 |
this.ShortenServiceConnector.DownloadStringCompleted += |
| 850 |
(sender, e) => |
| 851 |
{ |
| 852 |
if (e.Error != null) |
| 853 |
{ |
| 854 |
this.OnShortenURLFinished(new ShortenURLFinishedEventArgs(url, null, e.Error)); |
| 855 |
} |
| 856 |
else |
| 857 |
{ |
| 858 |
this.OnShortenURLFinished(new ShortenURLFinishedEventArgs(url, new Uri(e.Result), null)); |
| 859 |
} |
| 860 |
}; |
| 861 |
|
| 862 |
this.ShortenServiceConnector.DownloadStringAsync(new Uri(api)); |
| 863 |
} |
| 864 |
|
| 865 |
public void EndShortenURL() |
| 866 |
{ |
| 867 |
this.ShortenServiceConnector.CancelAsync(); |
| 868 |
} |
| 869 |
|
| 870 |
public void BeginExpandURL(Uri url) |
| 871 |
{ |
| 872 |
if (url == null) |
| 873 |
{ |
| 874 |
throw new ArgumentNullException("url"); |
| 875 |
} |
| 876 |
|
| 877 |
if (url.Host != "is.gd") |
| 878 |
{ |
| 879 |
throw new ArgumentException("is.gdによって短縮されたURLではありません。", "url"); |
| 880 |
} |
| 881 |
|
| 882 |
this.ExpandServiceConnector = new WebClient() |
| 883 |
{ |
| 884 |
Encoding = Encoding.UTF8 |
| 885 |
}; |
| 886 |
|
| 887 |
string api = String.Format("http://ux.nu/hugeurl?url={0}", url); |
| 888 |
|
| 889 |
this.ExpandServiceConnector.DownloadStringCompleted += |
| 890 |
(sender, e) => |
| 891 |
{ |
| 892 |
if (e.Error != null) |
| 893 |
{ |
| 894 |
this.OnExpandURLFinished(new ExpandURLFinishedEventArgs(url, null, e.Error)); |
| 895 |
} |
| 896 |
else |
| 897 |
{ |
| 898 |
var info = typeof(WebClient).GetField("m_WebResponse", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance); |
| 899 |
var response = info.GetValue(this.ExpandServiceConnector) as HttpWebResponse; |
| 900 |
|
| 901 |
this.OnExpandURLFinished(new ExpandURLFinishedEventArgs(url, response.ResponseUri, null)); |
| 902 |
} |
| 903 |
}; |
| 904 |
|
| 905 |
this.ExpandServiceConnector.DownloadStringAsync(new Uri(api)); |
| 906 |
} |
| 907 |
|
| 908 |
public void EndExpandURL() |
| 909 |
{ |
| 910 |
this.ExpandServiceConnector.CancelAsync(); |
| 911 |
} |
| 912 |
|
| 913 |
public void Dispose() |
| 914 |
{ |
| 915 |
this.ShortenServiceConnector.Dispose(); |
| 916 |
this.ExpandServiceConnector.Dispose(); |
| 917 |
} |
| 918 |
|
| 919 |
public event ShortenURLFinishedEventHandler ShortenURLFinished; |
| 920 |
|
| 921 |
public event ExpandURLFinishedEventHandler ExpandURLFinished; |
| 922 |
} |
| 923 |
|
| 924 |
/// <summary> |
| 925 |
/// tinyurl.comでのURL短縮用のメソッドを提供します。 |
| 926 |
/// </summary> |
| 927 |
public sealed class TinyurlComURLShortener : IURLShorten |
| 928 |
{ |
| 929 |
private WebClient ShortenServiceConnector; |
| 930 |
private WebClient ExpandServiceConnector; |
| 931 |
|
| 932 |
public TinyurlComURLShortener() |
| 933 |
{ |
| 934 |
} |
| 935 |
|
| 936 |
private void OnShortenURLFinished(ShortenURLFinishedEventArgs e) |
| 937 |
{ |
| 938 |
if (this.ShortenURLFinished != null) |
| 939 |
{ |
| 940 |
this.ShortenURLFinished(this, e); |
| 941 |
} |
| 942 |
} |
| 943 |
|
| 944 |
private void OnExpandURLFinished(ExpandURLFinishedEventArgs e) |
| 945 |
{ |
| 946 |
if (this.ExpandURLFinished != null) |
| 947 |
{ |
| 948 |
this.ExpandURLFinished(this, e); |
| 949 |
} |
| 950 |
} |
| 951 |
|
| 952 |
public Uri ShortenURL(Uri url) |
| 953 |
{ |
| 954 |
if (url == null) |
| 955 |
{ |
| 956 |
throw new ArgumentNullException("url"); |
| 957 |
} |
| 958 |
|
| 959 |
this.ShortenServiceConnector = new WebClient() |
| 960 |
{ |
| 961 |
Encoding = Encoding.UTF8 |
| 962 |
}; |
| 963 |
|
| 964 |
string api = String.Format("http://tinyurl.com/api-create.php?url={0}", url); |
| 965 |
|
| 966 |
string result = this.ShortenServiceConnector.DownloadString(api); |
| 967 |
|
| 968 |
return new Uri(result); |
| 969 |
} |
| 970 |
|
| 971 |
public Uri ExpandURL(Uri url) |
| 972 |
{ |
| 973 |
if (url == null) |
| 974 |
{ |
| 975 |
throw new ArgumentNullException("url"); |
| 976 |
} |
| 977 |
|
| 978 |
if (url.Host != "tinyurl.com") |
| 979 |
{ |
| 980 |
throw new ArgumentException("tinyurl.comによって短縮されたURLではありません。", "url"); |
| 981 |
} |
| 982 |
|
| 983 |
this.ExpandServiceConnector = new WebClient() |
| 984 |
{ |
| 985 |
Encoding = Encoding.UTF8 |
| 986 |
}; |
| 987 |
|
| 988 |
this.ExpandServiceConnector.DownloadString(url); |
| 989 |
|
| 990 |
var info = typeof(WebClient).GetField("m_WebResponse", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance); |
| 991 |
var response = info.GetValue(this.ExpandServiceConnector) as HttpWebResponse; |
| 992 |
|
| 993 |
return response.ResponseUri; |
| 994 |
} |
| 995 |
|
| 996 |
public void BeginShortenURL(Uri url) |
| 997 |
{ |
| 998 |
if (url == null) |
| 999 |
{ |
| 1000 |
throw new ArgumentNullException("url"); |
| 1001 |
} |
| 1002 |
|
| 1003 |
this.ShortenServiceConnector = new WebClient() |
| 1004 |
{ |
| 1005 |
Encoding = Encoding.UTF8 |
| 1006 |
}; |
| 1007 |
|
| 1008 |
string api = String.Format("http://tinyurl.com/api-create.php?url={0}", url); |
| 1009 |
|
| 1010 |
this.ShortenServiceConnector.DownloadStringCompleted += |
| 1011 |
(sender, e) => |
| 1012 |
{ |
| 1013 |
if (e.Error != null) |
| 1014 |
{ |
| 1015 |
this.OnShortenURLFinished(new ShortenURLFinishedEventArgs(url, null, e.Error)); |
| 1016 |
} |
| 1017 |
else |
| 1018 |
{ |
| 1019 |
this.OnShortenURLFinished(new ShortenURLFinishedEventArgs(url, new Uri(e.Result), null)); |
| 1020 |
} |
| 1021 |
}; |
| 1022 |
|
| 1023 |
this.ShortenServiceConnector.DownloadStringAsync(new Uri(api)); |
| 1024 |
} |
| 1025 |
|
| 1026 |
public void EndShortenURL() |
| 1027 |
{ |
| 1028 |
this.ShortenServiceConnector.CancelAsync(); |
| 1029 |
} |
| 1030 |
|
| 1031 |
public void BeginExpandURL(Uri url) |
| 1032 |
{ |
| 1033 |
if (url == null) |
| 1034 |
{ |
| 1035 |
throw new ArgumentNullException("url"); |
| 1036 |
} |
| 1037 |
|
| 1038 |
if (url.Host != "tinyurl.com") |
| 1039 |
{ |
| 1040 |
throw new ArgumentException("tinyurl.comによって短縮されたURLではありません。", "url"); |
| 1041 |
} |
| 1042 |
|
| 1043 |
this.ExpandServiceConnector = new WebClient() |
| 1044 |
{ |
| 1045 |
Encoding = Encoding.UTF8 |
| 1046 |
}; |
| 1047 |
|
| 1048 |
string api = String.Format("http://ux.nu/hugeurl?url={0}", url); |
| 1049 |
|
| 1050 |
this.ExpandServiceConnector.DownloadStringCompleted += |
| 1051 |
(sender, e) => |
| 1052 |
{ |
| 1053 |
if (e.Error != null) |
| 1054 |
{ |
| 1055 |
this.OnExpandURLFinished(new ExpandURLFinishedEventArgs(url, null, e.Error)); |
| 1056 |
} |
| 1057 |
else |
| 1058 |
{ |
| 1059 |
var info = typeof(WebClient).GetField("m_WebResponse", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance); |
| 1060 |
var response = info.GetValue(this.ExpandServiceConnector) as HttpWebResponse; |
| 1061 |
|
| 1062 |
this.OnExpandURLFinished(new ExpandURLFinishedEventArgs(url, response.ResponseUri, null)); |
| 1063 |
} |
| 1064 |
}; |
| 1065 |
|
| 1066 |
this.ExpandServiceConnector.DownloadStringAsync(new Uri(api)); |
| 1067 |
} |
| 1068 |
|
| 1069 |
public void EndExpandURL() |
| 1070 |
{ |
| 1071 |
this.ExpandServiceConnector.CancelAsync(); |
| 1072 |
} |
| 1073 |
|
| 1074 |
public void Dispose() |
| 1075 |
{ |
| 1076 |
this.ShortenServiceConnector.Dispose(); |
| 1077 |
this.ExpandServiceConnector.Dispose(); |
| 1078 |
} |
| 1079 |
|
| 1080 |
public event ShortenURLFinishedEventHandler ShortenURLFinished; |
| 1081 |
|
| 1082 |
public event ExpandURLFinishedEventHandler ExpandURLFinished; |
| 1083 |
} |
| 1084 |
|
| 1085 |
/// <summary> |
| 1086 |
/// twurl.nlでのURL短縮用のメソッドを提供します。 |
| 1087 |
/// </summary> |
| 1088 |
public sealed class TwurlNlURLShortener : IURLShorten |
| 1089 |
{ |
| 1090 |
private WebClient ShortenServiceConnector; |
| 1091 |
private WebClient ExpandServiceConnector; |
| 1092 |
|
| 1093 |
public TwurlNlURLShortener() |
| 1094 |
{ |
| 1095 |
} |
| 1096 |
|
| 1097 |
private void OnShortenURLFinished(ShortenURLFinishedEventArgs e) |
| 1098 |
{ |
| 1099 |
if (this.ShortenURLFinished != null) |
| 1100 |
{ |
| 1101 |
this.ShortenURLFinished(this, e); |
| 1102 |
} |
| 1103 |
} |
| 1104 |
|
| 1105 |
private void OnExpandURLFinished(ExpandURLFinishedEventArgs e) |
| 1106 |
{ |
| 1107 |
if (this.ExpandURLFinished != null) |
| 1108 |
{ |
| 1109 |
this.ExpandURLFinished(this, e); |
| 1110 |
} |
| 1111 |
} |
| 1112 |
|
| 1113 |
public Uri ShortenURL(Uri url) |
| 1114 |
{ |
| 1115 |
if (url == null) |
| 1116 |
{ |
| 1117 |
throw new ArgumentNullException("url"); |
| 1118 |
} |
| 1119 |
|
| 1120 |
this.ShortenServiceConnector = new WebClient() |
| 1121 |
{ |
| 1122 |
Encoding = Encoding.UTF8 |
| 1123 |
}; |
| 1124 |
|
| 1125 |
string api = "http://tweetburner.com/links"; |
| 1126 |
|
| 1127 |
NameValueCollection nvc = new NameValueCollection(); |
| 1128 |
nvc.Add("link[url]", url.ToString()); |
| 1129 |
|
| 1130 |
string result = Encoding.UTF8.GetString(this.ShortenServiceConnector.UploadValues(api, nvc)); |
| 1131 |
|
| 1132 |
return new Uri(result); |
| 1133 |
} |
| 1134 |
|
| 1135 |
public Uri ExpandURL(Uri url) |
| 1136 |
{ |
| 1137 |
if (url == null) |
| 1138 |
{ |
| 1139 |
throw new ArgumentNullException("url"); |
| 1140 |
} |
| 1141 |
|
| 1142 |
if (url.Host != "twurl.nl") |
| 1143 |
{ |
| 1144 |
throw new ArgumentException("twurl.nlによって短縮されたURLではありません。", "url"); |
| 1145 |
} |
| 1146 |
|
| 1147 |
this.ExpandServiceConnector = new WebClient() |
| 1148 |
{ |
| 1149 |
Encoding = Encoding.UTF8 |
| 1150 |
}; |
| 1151 |
|
| 1152 |
this.ExpandServiceConnector.DownloadString(url); |
| 1153 |
|
| 1154 |
var info = typeof(WebClient).GetField("m_WebResponse", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance); |
| 1155 |
var response = info.GetValue(this.ExpandServiceConnector) as HttpWebResponse; |
| 1156 |
|
| 1157 |
return response.ResponseUri; |
| 1158 |
} |
| 1159 |
|
| 1160 |
public void BeginShortenURL(Uri url) |
| 1161 |
{ |
| 1162 |
if (url == null) |
| 1163 |
{ |
| 1164 |
throw new ArgumentNullException("url"); |
| 1165 |
} |
| 1166 |
|
| 1167 |
this.ShortenServiceConnector = new WebClient() |
| 1168 |
{ |
| 1169 |
Encoding = Encoding.UTF8 |
| 1170 |
}; |
| 1171 |
|
| 1172 |
string api = String.Format("http://tinyurl.com/api-create.php?url={0}", url); |
| 1173 |
|
| 1174 |
this.ShortenServiceConnector.DownloadStringCompleted += |
| 1175 |
(sender, e) => |
| 1176 |
{ |
| 1177 |
if (e.Error != null) |
| 1178 |
{ |
| 1179 |
this.OnShortenURLFinished(new ShortenURLFinishedEventArgs(url, null, e.Error)); |
| 1180 |
} |
| 1181 |
else |
| 1182 |
{ |
| 1183 |
this.OnShortenURLFinished(new ShortenURLFinishedEventArgs(url, new Uri(e.Result), null)); |
| 1184 |
} |
| 1185 |
}; |
| 1186 |
|
| 1187 |
this.ShortenServiceConnector.DownloadStringAsync(new Uri(api)); |
| 1188 |
} |
| 1189 |
|
| 1190 |
public void EndShortenURL() |
| 1191 |
{ |
| 1192 |
this.ShortenServiceConnector.CancelAsync(); |
| 1193 |
} |
| 1194 |
|
| 1195 |
public void BeginExpandURL(Uri url) |
| 1196 |
{ |
| 1197 |
if (url == null) |
| 1198 |
{ |
| 1199 |
throw new ArgumentNullException("url"); |
| 1200 |
} |
| 1201 |
|
| 1202 |
if (url.Host != "twurl.nl") |
| 1203 |
{ |
| 1204 |
throw new ArgumentException("twurl.nlによって短縮されたURLではありません。", "url"); |
| 1205 |
} |
| 1206 |
|
| 1207 |
this.ExpandServiceConnector = new WebClient() |
| 1208 |
{ |
| 1209 |
Encoding = Encoding.UTF8 |
| 1210 |
}; |
| 1211 |
|
| 1212 |
string api = String.Format("http://ux.nu/hugeurl?url={0}", url); |
| 1213 |
|
| 1214 |
this.ExpandServiceConnector.DownloadStringCompleted += |
| 1215 |
(sender, e) => |
| 1216 |
{ |
| 1217 |
if (e.Error != null) |
| 1218 |
{ |
| 1219 |
this.OnExpandURLFinished(new ExpandURLFinishedEventArgs(url, null, e.Error)); |
| 1220 |
} |
| 1221 |
else |
| 1222 |
{ |
| 1223 |
var info = typeof(WebClient).GetField("m_WebResponse", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance); |
| 1224 |
var response = info.GetValue(this.ExpandServiceConnector) as HttpWebResponse; |
| 1225 |
|
| 1226 |
this.OnExpandURLFinished(new ExpandURLFinishedEventArgs(url, response.ResponseUri, null)); |
| 1227 |
} |
| 1228 |
}; |
| 1229 |
|
| 1230 |
this.ExpandServiceConnector.DownloadStringAsync(new Uri(api)); |
| 1231 |
} |
| 1232 |
|
| 1233 |
public void EndExpandURL() |
| 1234 |
{ |
| 1235 |
this.ExpandServiceConnector.CancelAsync(); |
| 1236 |
} |
| 1237 |
|
| 1238 |
public void Dispose() |
| 1239 |
{ |
| 1240 |
this.ShortenServiceConnector.Dispose(); |
| 1241 |
this.ExpandServiceConnector.Dispose(); |
| 1242 |
} |
| 1243 |
|
| 1244 |
public event ShortenURLFinishedEventHandler ShortenURLFinished; |
| 1245 |
|
| 1246 |
public event ExpandURLFinishedEventHandler ExpandURLFinished; |
| 1247 |
} |
| 1248 |
|
| 1249 |
/// <summary> |
| 1250 |
/// goo.glでのURL短縮用のメソッドを提供します。 |
| 1251 |
/// </summary> |
| 1252 |
public sealed class GooGlURLShortener : IURLShorten |
| 1253 |
{ |
| 1254 |
private WebClient ShortenServiceConnector; |
| 1255 |
private WebClient ExpandServiceConnector; |
| 1256 |
|
| 1257 |
public GooGlURLShortener() |
| 1258 |
{ |
| 1259 |
} |
| 1260 |
|
| 1261 |
private void OnShortenURLFinished(ShortenURLFinishedEventArgs e) |
| 1262 |
{ |
| 1263 |
if (this.ShortenURLFinished != null) |
| 1264 |
{ |
| 1265 |
this.ShortenURLFinished(this, e); |
| 1266 |
} |
| 1267 |
} |
| 1268 |
|
| 1269 |
private void OnExpandURLFinished(ExpandURLFinishedEventArgs e) |
| 1270 |
{ |
| 1271 |
if (this.ExpandURLFinished != null) |
| 1272 |
{ |
| 1273 |
this.ExpandURLFinished(this, e); |
| 1274 |
} |
| 1275 |
} |
| 1276 |
|
| 1277 |
public Uri ShortenURL(Uri url) |
| 1278 |
{ |
| 1279 |
if (url == null) |
| 1280 |
{ |
| 1281 |
throw new ArgumentNullException("url"); |
| 1282 |
} |
| 1283 |
|
| 1284 |
this.ShortenServiceConnector = new WebClient() |
| 1285 |
{ |
| 1286 |
Encoding = Encoding.UTF8 |
| 1287 |
}; |
| 1288 |
|
| 1289 |
string api = "http://goo.gl/api/url"; |
| 1290 |
|
| 1291 |
NameValueCollection nvc = new NameValueCollection(); |
| 1292 |
nvc.Add("user", "toolbar@google.com"); |
| 1293 |
nvc.Add("url", url.ToString()); |
| 1294 |
nvc.Add("auth_token", this.GenerateAuthToken(url.ToString())); |
| 1295 |
|
| 1296 |
string result = Encoding.UTF8.GetString(this.ShortenServiceConnector.UploadValues(api, nvc)); |
| 1297 |
|
| 1298 |
Regex pattern = new Regex("{\"short_url\":\"(.*?)\","); |
| 1299 |
Match match = pattern.Match(result); |
| 1300 |
|
| 1301 |
if (match.Success) |
| 1302 |
{ |
| 1303 |
return new Uri(match.Groups[1].Value); |
| 1304 |
} |
| 1305 |
else |
| 1306 |
{ |
| 1307 |
return null; |
| 1308 |
} |
| 1309 |
} |
| 1310 |
|
| 1311 |
public Uri ExpandURL(Uri url) |
| 1312 |
{ |
| 1313 |
if (url == null) |
| 1314 |
{ |
| 1315 |
throw new ArgumentNullException("url"); |
| 1316 |
} |
| 1317 |
|
| 1318 |
if (url.Host != "goo.gl") |
| 1319 |
{ |
| 1320 |
throw new ArgumentException("goo.glによって短縮されたURLではありません。", "url"); |
| 1321 |
} |
| 1322 |
|
| 1323 |
this.ExpandServiceConnector = new WebClient() |
| 1324 |
{ |
| 1325 |
Encoding = Encoding.UTF8 |
| 1326 |
}; |
| 1327 |
|
| 1328 |
this.ExpandServiceConnector.DownloadString(url); |
| 1329 |
|
| 1330 |
var info = typeof(WebClient).GetField("m_WebResponse", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance); |
| 1331 |
var response = info.GetValue(this.ExpandServiceConnector) as HttpWebResponse; |
| 1332 |
|
| 1333 |
return response.ResponseUri; |
| 1334 |
} |
| 1335 |
|
| 1336 |
public void BeginShortenURL(Uri url) |
| 1337 |
{ |
| 1338 |
if (url == null) |
| 1339 |
{ |
| 1340 |
throw new ArgumentNullException("url"); |
| 1341 |
} |
| 1342 |
|
| 1343 |
this.ShortenServiceConnector = new WebClient() |
| 1344 |
{ |
| 1345 |
Encoding = Encoding.UTF8 |
| 1346 |
}; |
| 1347 |
|
| 1348 |
string api = "http://goo.gl/api/url"; |
| 1349 |
|
| 1350 |
NameValueCollection nvc = new NameValueCollection(); |
| 1351 |
nvc.Add("user", "toolbar@google.com"); |
| 1352 |
nvc.Add("url", url.ToString()); |
| 1353 |
nvc.Add("auth_token", this.GenerateAuthToken(url.ToString())); |
| 1354 |
|
| 1355 |
this.ShortenServiceConnector.UploadValuesCompleted += |
| 1356 |
(sender, e) => |
| 1357 |
{ |
| 1358 |
if (e.Error != null) |
| 1359 |
{ |
| 1360 |
this.OnShortenURLFinished(new ShortenURLFinishedEventArgs(url, null, e.Error)); |
| 1361 |
} |
| 1362 |
else |
| 1363 |
{ |
| 1364 |
string result = Encoding.UTF8.GetString(e.Result); |
| 1365 |
|
| 1366 |
Regex pattern = new Regex("{\"short_url\":\"(.*?)\","); |
| 1367 |
Match match = pattern.Match(result); |
| 1368 |
|
| 1369 |
if (match.Success) |
| 1370 |
{ |
| 1371 |
this.OnShortenURLFinished(new ShortenURLFinishedEventArgs(url, new Uri(match.Groups[1].Value), null)); |
| 1372 |
} |
| 1373 |
else |
| 1374 |
{ |
| 1375 |
this.OnShortenURLFinished(new ShortenURLFinishedEventArgs(url, null, null)); |
| 1376 |
} |
| 1377 |
} |
| 1378 |
}; |
| 1379 |
|
| 1380 |
this.ShortenServiceConnector.UploadValuesAsync(new Uri(api), nvc); |
| 1381 |
} |
| 1382 |
|
| 1383 |
public void EndShortenURL() |
| 1384 |
{ |
| 1385 |
this.ShortenServiceConnector.CancelAsync(); |
| 1386 |
} |
| 1387 |
|
| 1388 |
public void BeginExpandURL(Uri url) |
| 1389 |
{ |
| 1390 |
if (url == null) |
| 1391 |
{ |
| 1392 |
throw new ArgumentNullException("url"); |
| 1393 |
} |
| 1394 |
|
| 1395 |
if (url.Host != "goo.gl") |
| 1396 |
{ |
| 1397 |
throw new ArgumentException("goo.glによって短縮されたURLではありません。", "url"); |
| 1398 |
} |
| 1399 |
|
| 1400 |
this.ExpandServiceConnector = new WebClient() |
| 1401 |
{ |
| 1402 |
Encoding = Encoding.UTF8 |
| 1403 |
}; |
| 1404 |
|
| 1405 |
string api = String.Format("http://ux.nu/hugeurl?url={0}", url); |
| 1406 |
|
| 1407 |
this.ExpandServiceConnector.DownloadStringCompleted += |
| 1408 |
(sender, e) => |
| 1409 |
{ |
| 1410 |
if (e.Error != null) |
| 1411 |
{ |
| 1412 |
this.OnExpandURLFinished(new ExpandURLFinishedEventArgs(url, null, e.Error)); |
| 1413 |
} |
| 1414 |
else |
| 1415 |
{ |
| 1416 |
var info = typeof(WebClient).GetField("m_WebResponse", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance); |
| 1417 |
var response = info.GetValue(this.ExpandServiceConnector) as HttpWebResponse; |
| 1418 |
|
| 1419 |
this.OnExpandURLFinished(new ExpandURLFinishedEventArgs(url, response.ResponseUri, null)); |
| 1420 |
} |
| 1421 |
}; |
| 1422 |
|
| 1423 |
this.ExpandServiceConnector.DownloadStringAsync(new Uri(api)); |
| 1424 |
} |
| 1425 |
|
| 1426 |
public void EndExpandURL() |
| 1427 |
{ |
| 1428 |
this.ExpandServiceConnector.CancelAsync(); |
| 1429 |
} |
| 1430 |
|
| 1431 |
public void Dispose() |
| 1432 |
{ |
| 1433 |
this.ShortenServiceConnector.Dispose(); |
| 1434 |
this.ExpandServiceConnector.Dispose(); |
| 1435 |
} |
| 1436 |
|
| 1437 |
private string GenerateAuthToken(string b) |
| 1438 |
{ |
| 1439 |
long i = __e(b); |
| 1440 |
|
| 1441 |
i = i >> 2 & 1073741823; |
| 1442 |
i = i >> 4 & 67108800 | i & 63; |
| 1443 |
i = i >> 4 & 4193280 | i & 1023; |
| 1444 |
i = i >> 4 & 245760 | i & 16383; |
| 1445 |
|
| 1446 |
long h = __f(b); |
| 1447 |
long k = (i >> 2 & 15) << 4 | h & 15; |
| 1448 |
k |= (i >> 6 & 15) << 12 | (h >> 8 & 15) << 8; |
| 1449 |
k |= (i >> 10 & 15) << 20 | (h >> 16 & 15) << 16; |
| 1450 |
k |= (i >> 14 & 15) << 28 | (h >> 24 & 15) << 24; |
| 1451 |
|
| 1452 |
return "7" + __d(k); |
| 1453 |
} |
| 1454 |
|
| 1455 |
private long __c(long a, long b, long c) |
| 1456 |
{ |
| 1457 |
long l = 0; |
| 1458 |
l += (a & 4294967295); |
| 1459 |
l += (b & 4294967295); |
| 1460 |
l += (c & 4294967295); |
| 1461 |
|
| 1462 |
return l; |
| 1463 |
} |
| 1464 |
|
| 1465 |
private long __c(long a, long b, long c, long d) |
| 1466 |
{ |
| 1467 |
long l = 0; |
| 1468 |
l += (a & 4294967295); |
| 1469 |
l += (b & 4294967295); |
| 1470 |
l += (c & 4294967295); |
| 1471 |
l += (d & 4294967295); |
| 1472 |
|
| 1473 |
return l; |
| 1474 |
} |
| 1475 |
|
| 1476 |
private string __d(long l) |
| 1477 |
{ |
| 1478 |
string ll = l.ToString(); |
| 1479 |
string m = (l > 0 ? l : l + 4294967296).ToString(); |
| 1480 |
bool n = false; |
| 1481 |
long o = 0; |
| 1482 |
|
| 1483 |
for (int p = m.Length - 1; p >= 0; --p) |
| 1484 |
{ |
| 1485 |
long q = Int64.Parse(m[p].ToString()); |
| 1486 |
|
| 1487 |
if (n) |
| 1488 |
{ |
| 1489 |
q *= 2; |
| 1490 |
o += (long)Math.Floor((double)q / 10) + q % 10; |
| 1491 |
} |
| 1492 |
else |
| 1493 |
{ |
| 1494 |
o += q; |
| 1495 |
} |
| 1496 |
|
| 1497 |
n = !n; |
| 1498 |
} |
| 1499 |
|
| 1500 |
long mm = o % 10; |
| 1501 |
|
| 1502 |
o = 0; |
| 1503 |
|
| 1504 |
if (mm != 0) |
| 1505 |
{ |
| 1506 |
o = 10 - mm; |
| 1507 |
|
| 1508 |
if (ll.Length % 2 == 1) |
| 1509 |
{ |
| 1510 |
if (o % 2 == 1) o += 9; |
| 1511 |
o /= 2; |
| 1512 |
} |
| 1513 |
} |
| 1514 |
|
| 1515 |
m = o.ToString(); |
| 1516 |
m += ll; |
| 1517 |
|
| 1518 |
return m; |
| 1519 |
} |
| 1520 |
|
| 1521 |
private long __e(string l) |
| 1522 |
{ |
| 1523 |
long m = 5381; |
| 1524 |
for (int o = 0; o < l.Length; o++) |
| 1525 |
{ |
| 1526 |
m = __c(m << 5, m, (long)l[o]); |
| 1527 |
} |
| 1528 |
return m; |
| 1529 |
} |
| 1530 |
|
| 1531 |
private long __f(string l) |
| 1532 |
{ |
| 1533 |
long m = 0; |
| 1534 |
for (int o = 0; o < l.Length; o++) |
| 1535 |
{ |
| 1536 |
m = __c(l[o], m << 6, m << 16, -m); |
| 1537 |
} |
| 1538 |
return m; |
| 1539 |
} |
| 1540 |
|
| 1541 |
public event ShortenURLFinishedEventHandler ShortenURLFinished; |
| 1542 |
|
| 1543 |
public event ExpandURLFinishedEventHandler ExpandURLFinished; |
| 1544 |
} |
| 1545 |
|
| 1546 |
/// <summary> |
| 1547 |
/// p.tlでのURL短縮用のメソッドを提供します。 |
| 1548 |
/// </summary> |
| 1549 |
public sealed class PTlURLShortener : IURLShorten |
| 1550 |
{ |
| 1551 |
private WebClient ShortenServiceConnector; |
| 1552 |
private WebClient ExpandServiceConnector; |
| 1553 |
|
| 1554 |
private string ApiKey; |
| 1555 |
|
| 1556 |
public PTlURLShortener(string apiKey) |
| 1557 |
{ |
| 1558 |
this.ApiKey = apiKey; |
| 1559 |
} |
| 1560 |
|
| 1561 |
private void OnShortenURLFinished(ShortenURLFinishedEventArgs e) |
| 1562 |
{ |
| 1563 |
if (this.ShortenURLFinished != null) |
| 1564 |
{ |
| 1565 |
this.ShortenURLFinished(this, e); |
| 1566 |
} |
| 1567 |
} |
| 1568 |
|
| 1569 |
private void OnExpandURLFinished(ExpandURLFinishedEventArgs e) |
| 1570 |
{ |
| 1571 |
if (this.ExpandURLFinished != null) |
| 1572 |
{ |
| 1573 |
this.ExpandURLFinished(this, e); |
| 1574 |
} |
| 1575 |
} |
| 1576 |
|
| 1577 |
public Uri ShortenURL(Uri url) |
| 1578 |
{ |
| 1579 |
if (url == null) |
| 1580 |
{ |
| 1581 |
throw new ArgumentNullException("url"); |
| 1582 |
} |
| 1583 |
|
| 1584 |
this.ShortenServiceConnector = new WebClient() |
| 1585 |
{ |
| 1586 |
Encoding = Encoding.UTF8 |
| 1587 |
}; |
| 1588 |
|
| 1589 |
string api = "http://p.tl/api/api_simple.php"; |
| 1590 |
|
| 1591 |
NameValueCollection nvc = new NameValueCollection(); |
| 1592 |
nvc.Add("url", url.ToString()); |
| 1593 |
nvc.Add("key", this.ApiKey); |
| 1594 |
|
| 1595 |
this.ShortenServiceConnector.QueryString = nvc; |
| 1596 |
string result = Encoding.UTF8.GetString(this.ShortenServiceConnector.UploadValues(api, nvc)); |
| 1597 |
|
| 1598 |
Regex pattern = new Regex("{\"status\":\"(.*?)\",\"long_url\":\"(.*?)\",\"short_url\":\"(.*?)\",\"counter\":(.*?)}"); |
| 1599 |
Match match = pattern.Match(result); |
| 1600 |
|
| 1601 |
if (match.Success) |
| 1602 |
{ |
| 1603 |
return new Uri(new string(match.Groups[3].Value.Where(c => c != '\\').ToArray())); |
| 1604 |
} |
| 1605 |
else |
| 1606 |
{ |
| 1607 |
return null; |
| 1608 |
} |
| 1609 |
} |
| 1610 |
|
| 1611 |
public Uri ExpandURL(Uri url) |
| 1612 |
{ |
| 1613 |
if (url == null) |
| 1614 |
{ |
| 1615 |
throw new ArgumentNullException("url"); |
| 1616 |
} |
| 1617 |
|
| 1618 |
if (url.Host != "p.tl") |
| 1619 |
{ |
| 1620 |
throw new ArgumentException("p.tlによって短縮されたURLではありません。", "url"); |
| 1621 |
} |
| 1622 |
|
| 1623 |
this.ExpandServiceConnector = new WebClient() |
| 1624 |
{ |
| 1625 |
Encoding = Encoding.UTF8 |
| 1626 |
}; |
| 1627 |
|
| 1628 |
this.ExpandServiceConnector.DownloadString(url); |
| 1629 |
|
| 1630 |
var info = typeof(WebClient).GetField("m_WebResponse", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance); |
| 1631 |
var response = info.GetValue(this.ExpandServiceConnector) as HttpWebResponse; |
| 1632 |
|
| 1633 |
return response.ResponseUri; |
| 1634 |
} |
| 1635 |
|
| 1636 |
public void BeginShortenURL(Uri url) |
| 1637 |
{ |
| 1638 |
if (url == null) |
| 1639 |
{ |
| 1640 |
throw new ArgumentNullException("url"); |
| 1641 |
} |
| 1642 |
|
| 1643 |
this.ShortenServiceConnector = new WebClient() |
| 1644 |
{ |
| 1645 |
Encoding = Encoding.UTF8 |
| 1646 |
}; |
| 1647 |
|
| 1648 |
string api = String.Format("http://tinyurl.com/api-create.php?url={0}", url); |
| 1649 |
|
| 1650 |
NameValueCollection nvc = new NameValueCollection(); |
| 1651 |
nvc.Add("url", url.ToString()); |
| 1652 |
nvc.Add("key", this.ApiKey); |
| 1653 |
|
| 1654 |
this.ShortenServiceConnector.UploadValuesCompleted += |
| 1655 |
(sender, e) => |
| 1656 |
{ |
| 1657 |
if (e.Error != null) |
| 1658 |
{ |
| 1659 |
this.OnShortenURLFinished(new ShortenURLFinishedEventArgs(url, null, e.Error)); |
| 1660 |
} |
| 1661 |
else |
| 1662 |
{ |
| 1663 |
string result = Encoding.UTF8.GetString(e.Result); |
| 1664 |
Regex pattern = new Regex("{\"status\":\"(.*?)\",\"long_url\":\"(.*?)\",\"short_url\":\"(.*?)\",\"counter\":(.*?)}"); |
| 1665 |
Match match = pattern.Match(result); |
| 1666 |
|
| 1667 |
if (match.Success) |
| 1668 |
{ |
| 1669 |
this.OnShortenURLFinished(new ShortenURLFinishedEventArgs(url, new Uri(new string(match.Groups[3].Value.Where(c => c != '\\').ToArray())), null)); |
| 1670 |
} |
| 1671 |
else |
| 1672 |
{ |
| 1673 |
this.OnShortenURLFinished(new ShortenURLFinishedEventArgs(url, null, null)); |
| 1674 |
} |
| 1675 |
} |
| 1676 |
}; |
| 1677 |
|
| 1678 |
this.ShortenServiceConnector.QueryString = nvc; |
| 1679 |
this.ShortenServiceConnector.UploadValuesAsync(new Uri(api), nvc); |
| 1680 |
} |
| 1681 |
|
| 1682 |
public void EndShortenURL() |
| 1683 |
{ |
| 1684 |
this.ShortenServiceConnector.CancelAsync(); |
| 1685 |
} |
| 1686 |
|
| 1687 |
public void BeginExpandURL(Uri url) |
| 1688 |
{ |
| 1689 |
if (url == null) |
| 1690 |
{ |
| 1691 |
throw new ArgumentNullException("url"); |
| 1692 |
} |
| 1693 |
|
| 1694 |
if (url.Host != "p.tl") |
| 1695 |
{ |
| 1696 |
throw new ArgumentException("p.tlによって短縮されたURLではありません。", "url"); |
| 1697 |
} |
| 1698 |
|
| 1699 |
this.ExpandServiceConnector = new WebClient() |
| 1700 |
{ |
| 1701 |
Encoding = Encoding.UTF8 |
| 1702 |
}; |
| 1703 |
|
| 1704 |
string api = String.Format("http://ux.nu/hugeurl?url={0}", url); |
| 1705 |
|
| 1706 |
this.ExpandServiceConnector.DownloadStringCompleted += |
| 1707 |
(sender, e) => |
| 1708 |
{ |
| 1709 |
if (e.Error != null) |
| 1710 |
{ |
| 1711 |
this.OnExpandURLFinished(new ExpandURLFinishedEventArgs(url, null, e.Error)); |
| 1712 |
} |
| 1713 |
else |
| 1714 |
{ |
| 1715 |
var info = typeof(WebClient).GetField("m_WebResponse", BindingFlags.GetField | BindingFlags.NonPublic | BindingFlags.Instance); |
| 1716 |
var response = info.GetValue(this.ExpandServiceConnector) as HttpWebResponse; |
| 1717 |
|
| 1718 |
this.OnExpandURLFinished(new ExpandURLFinishedEventArgs(url, response.ResponseUri, null)); |
| 1719 |
} |
| 1720 |
}; |
| 1721 |
|
| 1722 |
this.ExpandServiceConnector.DownloadStringAsync(new Uri(api)); |
| 1723 |
} |
| 1724 |
|
| 1725 |
public void EndExpandURL() |
| 1726 |
{ |
| 1727 |
this.ExpandServiceConnector.CancelAsync(); |
| 1728 |
} |
| 1729 |
|
| 1730 |
public void Dispose() |
| 1731 |
{ |
| 1732 |
this.ShortenServiceConnector.Dispose(); |
| 1733 |
this.ExpandServiceConnector.Dispose(); |
| 1734 |
} |
| 1735 |
|
| 1736 |
public event ShortenURLFinishedEventHandler ShortenURLFinished; |
| 1737 |
|
| 1738 |
public event ExpandURLFinishedEventHandler ExpandURLFinished; |
| 1739 |
} |
| 1740 |
|
| 1741 |
/// <summary> |
| 1742 |
/// IURLShortenを実装するオブジェクトを利用するための一連のstaticメソッドを提供します。 |
| 1743 |
/// </summary> |
| 1744 |
public static class URLShortener |
| 1745 |
{ |
| 1746 |
public static Uri ShortenByBitLy(this Uri url, string user, string apiKey) |
| 1747 |
{ |
| 1748 |
using (BitLyURLShortener sh = new BitLyURLShortener(user, apiKey)) |
| 1749 |
{ |
| 1750 |
return sh.ShortenURL(url); |
| 1751 |
} |
| 1752 |
} |
| 1753 |
|
| 1754 |
public static Uri ShortenByJMp(this Uri url, string user, string apiKey) |
| 1755 |
{ |
| 1756 |
using (JMpURLShortener sh = new JMpURLShortener(user, apiKey)) |
| 1757 |
{ |
| 1758 |
return sh.ShortenURL(url); |
| 1759 |
} |
| 1760 |
} |
| 1761 |
|
| 1762 |
public static Uri ShortenByUxNu(this Uri url) |
| 1763 |
{ |
| 1764 |
using (UxNuURLShortener sh = new UxNuURLShortener()) |
| 1765 |
{ |
| 1766 |
return sh.ShortenURL(url); |
| 1767 |
} |
| 1768 |
} |
| 1769 |
|
| 1770 |
public static Uri ShortenByIsGd(this Uri url) |
| 1771 |
{ |
| 1772 |
using (IsGdURLShortener sh = new IsGdURLShortener()) |
| 1773 |
{ |
| 1774 |
return sh.ShortenURL(url); |
| 1775 |
} |
| 1776 |
} |
| 1777 |
|
| 1778 |
public static Uri ShortenByTinyurlCom(this Uri url) |
| 1779 |
{ |
| 1780 |
using (TinyurlComURLShortener sh = new TinyurlComURLShortener()) |
| 1781 |
{ |
| 1782 |
return sh.ShortenURL(url); |
| 1783 |
} |
| 1784 |
} |
| 1785 |
|
| 1786 |
public static Uri ShortenByTwurlNl(this Uri url) |
| 1787 |
{ |
| 1788 |
using (TwurlNlURLShortener sh = new TwurlNlURLShortener()) |
| 1789 |
{ |
| 1790 |
return sh.ShortenURL(url); |
| 1791 |
} |
| 1792 |
} |
| 1793 |
|
| 1794 |
public static Uri ShortenByGooGl(this Uri url) |
| 1795 |
{ |
| 1796 |
using (GooGlURLShortener sh = new GooGlURLShortener()) |
| 1797 |
{ |
| 1798 |
return sh.ShortenURL(url); |
| 1799 |
} |
| 1800 |
} |
| 1801 |
|
| 1802 |
public static Uri ShortenByPTl(this Uri url, string apiKey) |
| 1803 |
{ |
| 1804 |
using (PTlURLShortener sh = new PTlURLShortener(apiKey)) |
| 1805 |
{ |
| 1806 |
return sh.ShortenURL(url); |
| 1807 |
} |
| 1808 |
} |
| 1809 |
|
| 1810 |
public static Uri ExpandFromBitLy(this Uri url, string user, string apiKey) |
| 1811 |
{ |
| 1812 |
using (BitLyURLShortener sh = new BitLyURLShortener(user, apiKey)) |
| 1813 |
{ |
| 1814 |
return sh.ExpandURL(url); |
| 1815 |
} |
| 1816 |
} |
| 1817 |
|
| 1818 |
public static Uri ExpandFromJMp(this Uri url, string user, string apiKey) |
| 1819 |
{ |
| 1820 |
using (JMpURLShortener sh = new JMpURLShortener(user, apiKey)) |
| 1821 |
{ |
| 1822 |
return sh.ExpandURL(url); |
| 1823 |
} |
| 1824 |
} |
| 1825 |
|
| 1826 |
public static Uri ExpandFromUxNu(this Uri url) |
| 1827 |
{ |
| 1828 |
using (UxNuURLShortener sh = new UxNuURLShortener()) |
| 1829 |
{ |
| 1830 |
return sh.ExpandURL(url); |
| 1831 |
} |
| 1832 |
} |
| 1833 |
|
| 1834 |
public static Uri ExpandFromIsGd(this Uri url) |
| 1835 |
{ |
| 1836 |
using (IsGdURLShortener sh = new IsGdURLShortener()) |
| 1837 |
{ |
| 1838 |
return sh.ExpandURL(url); |
| 1839 |
} |
| 1840 |
} |
| 1841 |
|
| 1842 |
public static Uri ExpandFromTinyurlCom(this Uri url) |
| 1843 |
{ |
| 1844 |
using (TinyurlComURLShortener sh = new TinyurlComURLShortener()) |
| 1845 |
{ |
| 1846 |
return sh.ExpandURL(url); |
| 1847 |
} |
| 1848 |
} |
| 1849 |
|
| 1850 |
public static Uri ExpandFromTwurlNl(this Uri url) |
| 1851 |
{ |
| 1852 |
using (TwurlNlURLShortener sh = new TwurlNlURLShortener()) |
| 1853 |
{ |
| 1854 |
return sh.ExpandURL(url); |
| 1855 |
} |
| 1856 |
} |
| 1857 |
|
| 1858 |
public static Uri ExpandFromGooGl(this Uri url) |
| 1859 |
{ |
| 1860 |
using (GooGlURLShortener sh = new GooGlURLShortener()) |
| 1861 |
{ |
| 1862 |
return sh.ExpandURL(url); |
| 1863 |
} |
| 1864 |
} |
| 1865 |
|
| 1866 |
public static Uri ExpandFromPTl(this Uri url, string apiKey) |
| 1867 |
{ |
| 1868 |
using (PTlURLShortener sh = new PTlURLShortener(apiKey)) |
| 1869 |
{ |
| 1870 |
return sh.ExpandURL(url); |
| 1871 |
} |
| 1872 |
} |
| 1873 |
} |
| 1874 |
} |