Created
March 31, 2016 09:09
-
-
Save aykuttasil/0d6b0553bf521a838b07b83a130ab543 to your computer and use it in GitHub Desktop.
Short Url
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class ShortUrlRequest { public string longUrl { get; set; } } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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