comparison Agendas/trunk/src/Agendas.Google/GUrlShortenerAdapter.cs @ 190:e6abe8e1a794

Mejoras menores en shorten url
author nelopauselli
date Tue, 09 Aug 2011 09:45:07 -0300
parents fe47f11f5f20
children 806fd94727ce
comparison
equal deleted inserted replaced
189:fe47f11f5f20 190:e6abe8e1a794
16 public string GetShortUrl(string url) 16 public string GetShortUrl(string url)
17 { 17 {
18 WebRequest request = WebRequest.Create(_config.UrlAPI); 18 WebRequest request = WebRequest.Create(_config.UrlAPI);
19 request.Method = WebRequestMethods.Http.Post; 19 request.Method = WebRequestMethods.Http.Post;
20 request.ContentType = "application/json"; 20 request.ContentType = "application/json";
21 21
22 using(var writer = new StreamWriter(request.GetRequestStream())) 22 using (var writer = new StreamWriter(request.GetRequestStream()))
23 { 23 {
24 var json = string.Format("{{\"longUrl\": \"{0}\"}}", url); 24 var json = string.Format("{{\"longUrl\": \"{0}\"}}", url);
25 writer.Write(json); 25 writer.Write(json);
26 } 26 }
27 27
28 var response = request.GetResponse(); 28 var response = request.GetResponse() as HttpWebResponse;
29 29
30 using(var reader = new StreamReader(response.GetResponseStream())) 30 if (response != null && response.StatusCode == HttpStatusCode.OK)
31 { 31 {
32 const string prefix = "\"id\": \""; 32 var stream = response.GetResponseStream();
33 33
34 var json = reader.ReadToEnd(); 34 if (stream != null)
35 {
36 using (var reader = new StreamReader(stream))
37 {
38 const string prefix = "\"id\": \"";
35 39
36 var start = json.IndexOf(prefix) + prefix.Length; 40 var json = reader.ReadToEnd();
37 if (start>prefix.Length)
38 {
39 var length = json.IndexOf("\",", start) - start;
40 41
41 if (length>0) 42 var start = json.IndexOf(prefix) + prefix.Length;
42 return json.Substring(start, length); 43 if (start > prefix.Length)
44 {
45 var length = json.IndexOf("\",", start) - start;
46
47 if (length > 0)
48 return json.Substring(start, length);
49 }
50 }
43 } 51 }
44 } 52 }
53
45 return string.Empty; 54 return string.Empty;
46 } 55 }
47 } 56 }
48 } 57 }