Mercurial > altnet-hispano
annotate Agendas/trunk/src/Agendas.Google/GUrlShortenerAdapter.cs @ 302:11dddcc9a862 tip
Historico de Eventos, no muestra bien la Url del Patrocinador.
author | juanjose.montesdeocaarbos |
---|---|
date | Tue, 14 Aug 2012 21:54:30 -0300 |
parents | 806fd94727ce |
children |
rev | line source |
---|---|
232
806fd94727ce
Validación de parámetros en GUrlShortener
juanjose.montesdeocaarbos
parents:
190
diff
changeset
|
1 using System; |
806fd94727ce
Validación de parámetros en GUrlShortener
juanjose.montesdeocaarbos
parents:
190
diff
changeset
|
2 using System.IO; |
189 | 3 using System.Net; |
4 using AltNetHispano.Agendas.Configurations; | |
5 | |
6 namespace AltNetHispano.Agendas.Google | |
7 { | |
8 public class GUrlShortenerAdapter | |
9 { | |
10 private readonly ShortenConfigurationElement _config; | |
11 | |
12 public GUrlShortenerAdapter() | |
13 { | |
14 _config = AgendasConfigurationManager.Publicadores.Google.Shorten; | |
15 } | |
16 | |
17 public string GetShortUrl(string url) | |
18 { | |
232
806fd94727ce
Validación de parámetros en GUrlShortener
juanjose.montesdeocaarbos
parents:
190
diff
changeset
|
19 if (url == null) |
806fd94727ce
Validación de parámetros en GUrlShortener
juanjose.montesdeocaarbos
parents:
190
diff
changeset
|
20 throw new ArgumentNullException("url"); |
806fd94727ce
Validación de parámetros en GUrlShortener
juanjose.montesdeocaarbos
parents:
190
diff
changeset
|
21 else if (string.IsNullOrWhiteSpace(url)) |
806fd94727ce
Validación de parámetros en GUrlShortener
juanjose.montesdeocaarbos
parents:
190
diff
changeset
|
22 throw new ArgumentException(); |
806fd94727ce
Validación de parámetros en GUrlShortener
juanjose.montesdeocaarbos
parents:
190
diff
changeset
|
23 |
189 | 24 WebRequest request = WebRequest.Create(_config.UrlAPI); |
25 request.Method = WebRequestMethods.Http.Post; | |
26 request.ContentType = "application/json"; | |
190 | 27 |
28 using (var writer = new StreamWriter(request.GetRequestStream())) | |
189 | 29 { |
30 var json = string.Format("{{\"longUrl\": \"{0}\"}}", url); | |
31 writer.Write(json); | |
32 } | |
33 | |
190 | 34 var response = request.GetResponse() as HttpWebResponse; |
189 | 35 |
190 | 36 if (response != null && response.StatusCode == HttpStatusCode.OK) |
189 | 37 { |
190 | 38 var stream = response.GetResponseStream(); |
189 | 39 |
190 | 40 if (stream != null) |
41 { | |
42 using (var reader = new StreamReader(stream)) | |
43 { | |
44 const string prefix = "\"id\": \""; | |
189 | 45 |
190 | 46 var json = reader.ReadToEnd(); |
189 | 47 |
190 | 48 var start = json.IndexOf(prefix) + prefix.Length; |
49 if (start > prefix.Length) | |
50 { | |
51 var length = json.IndexOf("\",", start) - start; | |
52 | |
53 if (length > 0) | |
54 return json.Substring(start, length); | |
55 } | |
56 } | |
189 | 57 } |
58 } | |
190 | 59 |
189 | 60 return string.Empty; |
61 } | |
62 } | |
63 } |