1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 | using System; using System; using System.IO; using System.Net; using System.Text.RegularExpressions; namespace Google_Short_URL { class ShortURL { static void Main( string [] args ) { // 티스토리 에디터에서 <id> 부분을 HTML 태그로 인식해서 // 자동으로 </id> 태그 붙임. // 정규식 으로 반환된 짧은 URL 캡쳐 const string MATCH_PATTERN = @"""id"": ?""(?<id>.+)""" ; //var httpWebRequest = (HttpWebRequest)WebRequest.Create( "https://www.googleapis.com/urlshortener/v1/url" ); httpWebRequest.ContentType = "application/json" ; httpWebRequest.Method = "POST" ; using (StreamWriter streamWriter = new StreamWriter( httpWebRequest.GetRequestStream() )) { //string json = "{\"longUrl\":\"http://www.google.com/\"}"; string json = "{\"longUrl\":\"" + myUrl + "\"}" ; // 긴 URL 출력 Console.WriteLine( "긴 URL : {0}\n" , json ); streamWriter.Write( json ); } //var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse(); WebResponse httpResponse = httpWebRequest.GetResponse(); using (StreamReader streamReader = new StreamReader( httpResponse.GetResponseStream() )) { var responseText = streamReader.ReadToEnd(); Console.WriteLine( "{0}\n" , responseText ); Console.WriteLine( "반환된 짧은 URL : {0}\n" , Regex.Match( responseText, MATCH_PATTERN ).Groups[ "id" ].Value ); } } } } // 티스토리게시판 태그 에러 </id> |
'C#' 카테고리의 다른 글
C# WebClient로 파일 다운 로드가 안될때 (0) | 2015.06.09 |
---|---|
비주얼 스튜디오 설치후 추가 확장 애드인 (0) | 2015.05.20 |
HighLight.JS 티스토리에서 사용 하기. (0) | 2014.07.06 |
티스토리에서 브라우저 API Key 구글 ShortURL 호출 (0) | 2014.07.05 |
테스트 스크립트 (0) | 2014.06.23 |