'Google Short URL'에 해당되는 글 2건

  1. 2014.07.05 티스토리에서 브라우저 API Key 구글 ShortURL 호출
  2. 2014.06.24 C# 구글 ShortURL
URL:




URL:


Wait. Loading....


 

 
URL:  




URL:


Wait. Loading....
Posted by seanpaul
,

C# 구글 ShortURL

C# 2014. 6. 24. 05:01
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 )
        {
            // 티스토리 에디터에서  부분을 HTML 태그로 인식해서 
            // 자동으로  태그 붙임.

            // 정규식 으로 반환된 짧은 URL 캡쳐
            const string MATCH_PATTERN = @"""id"": ?""(?.+)"""; 

            //var httpWebRequest = (HttpWebRequest)WebRequest.Create( "https://www.googleapis.com/urlshortener/v1/url" );
            WebRequest 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 myUrl = "https://developers.google.com/url-shortener/v1/getting_started?hl=ko";
                //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 );
            }
        }
    }
}
// 티스토리게시판 태그 에러
Posted by seanpaul
,