如何在谷歌的URL Shortener中拥有用户输入网址?

时间:2015-01-19 03:40:26

标签: c# .net goo.gl

所以我正在制作一个有趣的控制台应用程序,用户在网址中键入并返回缩短版本。所以,当我说:storage =" {\" longUrl \":\" https://www.facebook.com/ \"}&#34 ;; //存储网站/链接网址 而不是facebook.com如何使用urlString?

 using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Google.Apis.Urlshortener;
    using System.Net;
    using System.IO;

   namespace GoogleTest
   {
    class urlShortener
    {
        private string urlString; //Contains string of the URL
        private string nameOfurl; //Generic URL name
        private string shortenedURLstring;
        private string storage;

        public urlShortener(string nameOfurl, string urlString) //create new shortened URL
        {
            var httpWebRequest = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url");
            httpWebRequest.ContentType = "application/json";
            httpWebRequest.Method = "POST";

            this.nameOfurl = nameOfurl;
            this.urlString = urlString;

            using (var streamWriter = new StreamWriter(httpWebRequest.GetRequestStream()))
            {
                storage= "{\"longUrl\":\"https://www.facebook.com/\"}"; //Store website/link URL 
                Console.WriteLine(storage);
                streamWriter.Write(storage);
            }

            var httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
            using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
            {
                var responseText = streamReader.ReadToEnd();
                Console.WriteLine(responseText);
            }
        }
    }
}

0 个答案:

没有答案
相关问题