Bir oyun için yapılan projede ürünlerin idlerini kendi hostuma kaydeyip ürünü alanlara bunun linkini veriyorum onlarda settingslerden ayarlıyorlar. Fakat burda soyle bir durum var oyun tarafında işlem yapabilemk için url ile bazı bilgiler taşımam gerekiyor bu da haliyle çok uzun bir link oluyor Google ın URL Shortener API sini kullanarak bu durumu düzeltebiliriz.
public class GoogleUrlShortnerApi
{
private const string key = "kendi apiniz";
//https://code.google.com/apis/console/ adresinden api keyinizi alabilirsiniz
public static string Shorten(string url)
{
string post = "{\"longUrl\": \"" + url + "\"}";
string shortUrl = url;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create("https://www.googleapis.com/urlshortener/v1/url?key=" + key);
try
{
request.ServicePoint.Expect100Continue = false;
request.Method = "POST";
request.ContentLength = post.Length;
request.ContentType = "application/json";
request.Headers.Add("Cache-Control", "no-cache");
using (Stream requestStream = request.GetRequestStream())
{
byte[] postBuffer = Encoding.ASCII.GetBytes(post);
requestStream.Write(postBuffer, 0, postBuffer.Length);
}
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
{
using (Stream responseStream = response.GetResponseStream())
{
using (StreamReader responseReader = new StreamReader(responseStream))
{
string json = responseReader.ReadToEnd();
shortUrl = Regex.Match(json, @"""id"": ?""(?.+)""").Groups["id"].Value;
}
}
}
}
catch (Exception ex)
{
// if Google's URL Shortner is down...
System.Diagnostics.Debug.WriteLine(ex.Message);
System.Diagnostics.Debug.WriteLine(ex.StackTrace);
}
return shortUrl;
string url = GoogleUrlShortnerApi.Shorten("http://adasddasdda.com/Content/Data/Keys/76a214c5-18ac-ace4-219d-278bbbd614c3.txt");
Sonuç:
Eski:http://adasddasdda.com/Content/Data/Keys/76a214c5-18ac-ace4-219d-278bbbd614c3.txt
Yeni: http://goo.gl/Wtnm9
