所以我制作了一个有趣的控制台应用程序,用户输入一个 url 并返回它的缩短版本。所以当我说: storage= "{\"longUrl\":\" https://www.facebook.com/ \"}"; //存储网站/链接 URL 而不是 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);
}
}
}
}