changeset 190:e6abe8e1a794

Mejoras menores en shorten url
author nelopauselli
date Tue, 09 Aug 2011 09:45:07 -0300
parents fe47f11f5f20
children 03c237cd5cc4
files Agendas/trunk/src/Agendas.Google.Test/GUrlShortenerTest.cs Agendas/trunk/src/Agendas.Google/GUrlShortenerAdapter.cs
diffstat 2 files changed, 22 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/Agendas/trunk/src/Agendas.Google.Test/GUrlShortenerTest.cs	Tue Aug 09 09:38:28 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Google.Test/GUrlShortenerTest.cs	Tue Aug 09 09:45:07 2011 -0300
@@ -5,11 +5,10 @@
 namespace Agendas.Google.Test
 {
 	[TestFixture]
-	[Explicit]
 	public class GUrlShortenerTest
 	{
 		[Test]
-		public void Obtener_Url_corta()
+		public void Obtener_url_corta()
 		{
 			var gUrlShortener = new GUrlShortenerAdapter();
 
--- a/Agendas/trunk/src/Agendas.Google/GUrlShortenerAdapter.cs	Tue Aug 09 09:38:28 2011 -0300
+++ b/Agendas/trunk/src/Agendas.Google/GUrlShortenerAdapter.cs	Tue Aug 09 09:45:07 2011 -0300
@@ -18,30 +18,39 @@
 			WebRequest request = WebRequest.Create(_config.UrlAPI);
 			request.Method = WebRequestMethods.Http.Post;
 			request.ContentType = "application/json";
-			
-			using(var writer = new StreamWriter(request.GetRequestStream()))
+
+			using (var writer = new StreamWriter(request.GetRequestStream()))
 			{
 				var json = string.Format("{{\"longUrl\": \"{0}\"}}", url);
 				writer.Write(json);
 			}
 
-			var response = request.GetResponse();
+			var response = request.GetResponse() as HttpWebResponse;
 
-			using(var reader = new StreamReader(response.GetResponseStream()))
+			if (response != null && response.StatusCode == HttpStatusCode.OK)
 			{
-				const string prefix = "\"id\": \"";
+				var stream = response.GetResponseStream();
 
-				var json = reader.ReadToEnd();
+				if (stream != null)
+				{
+					using (var reader = new StreamReader(stream))
+					{
+						const string prefix = "\"id\": \"";
 
-				var start = json.IndexOf(prefix) + prefix.Length;
-				if (start>prefix.Length)
-				{
-					var length = json.IndexOf("\",", start) - start;
+						var json = reader.ReadToEnd();
 
-					if (length>0)
-						return json.Substring(start, length);
+						var start = json.IndexOf(prefix) + prefix.Length;
+						if (start > prefix.Length)
+						{
+							var length = json.IndexOf("\",", start) - start;
+
+							if (length > 0)
+								return json.Substring(start, length);
+						}
+					}
 				}
 			}
+
 			return string.Empty;
 		}
 	}