'구글 짧은 주소'에 해당되는 글 1건

  1. 2014.06.24 C# 구글 ShortURL

C# 구글 ShortURL

C# 2014. 6. 24. 05:01
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" );
            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 );
            }
        }
    }
}
// 티스토리게시판 태그 에러
</id>
Posted by seanpaul
,