Skip to content

Instantly share code, notes, and snippets.

@aykuttasil
Created March 31, 2016 09:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aykuttasil/0d6b0553bf521a838b07b83a130ab543 to your computer and use it in GitHub Desktop.
Save aykuttasil/0d6b0553bf521a838b07b83a130ab543 to your computer and use it in GitHub Desktop.
Short Url
private ShortUrlResponse GetShortUrl(ShortUrlRequest shortUrlRequest)
{
var postString = JsonConvert.SerializeObject(shortUrlRequest);
ShortUrlResponse response = null;
var urlpath = "https://www.googleapis.com/urlshortener/v1/url?key=" + ConfigurationManager.AppSettings["ShortUrlKey"];
var httpWebRequest = (HttpWebRequest)WebRequest.Create(urlpath);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "POST";
var requestWriter = new StreamWriter(httpWebRequest.GetRequestStream());
requestWriter.Write(postString);
requestWriter.Close();
var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
var responseText = streamReader.ReadToEnd();
response = JsonConvert.DeserializeObject<ShortUrlResponse>(responseText);
}
return response;
}
public class ShortUrlRequest { public string longUrl { get; set; } }
public class ShortUrlResponse { public string id { get; set; } public string kind { get; set; } public string longUrl { get; set; } }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment