Mercurial > altnet-hispano
comparison Agendas/trunk/src/Agendas.Google/GUrlShortenerAdapter.cs @ 189:fe47f11f5f20
Adapter para url shorten de google
author | nelopauselli |
---|---|
date | Tue, 09 Aug 2011 09:38:28 -0300 |
parents | |
children | e6abe8e1a794 |
comparison
equal
deleted
inserted
replaced
188:49125b681709 | 189:fe47f11f5f20 |
---|---|
1 using System.IO; | |
2 using System.Net; | |
3 using AltNetHispano.Agendas.Configurations; | |
4 | |
5 namespace AltNetHispano.Agendas.Google | |
6 { | |
7 public class GUrlShortenerAdapter | |
8 { | |
9 private readonly ShortenConfigurationElement _config; | |
10 | |
11 public GUrlShortenerAdapter() | |
12 { | |
13 _config = AgendasConfigurationManager.Publicadores.Google.Shorten; | |
14 } | |
15 | |
16 public string GetShortUrl(string url) | |
17 { | |
18 WebRequest request = WebRequest.Create(_config.UrlAPI); | |
19 request.Method = WebRequestMethods.Http.Post; | |
20 request.ContentType = "application/json"; | |
21 | |
22 using(var writer = new StreamWriter(request.GetRequestStream())) | |
23 { | |
24 var json = string.Format("{{\"longUrl\": \"{0}\"}}", url); | |
25 writer.Write(json); | |
26 } | |
27 | |
28 var response = request.GetResponse(); | |
29 | |
30 using(var reader = new StreamReader(response.GetResponseStream())) | |
31 { | |
32 const string prefix = "\"id\": \""; | |
33 | |
34 var json = reader.ReadToEnd(); | |
35 | |
36 var start = json.IndexOf(prefix) + prefix.Length; | |
37 if (start>prefix.Length) | |
38 { | |
39 var length = json.IndexOf("\",", start) - start; | |
40 | |
41 if (length>0) | |
42 return json.Substring(start, length); | |
43 } | |
44 } | |
45 return string.Empty; | |
46 } | |
47 } | |
48 } |