I found the following code to create a tinyurl.com url:
http://tinyurl.com/api-create.php?url=http://myurl.com
This will automatically create a tinyurl url. Is there a way to do this using code, specifically C# in ASP.NET?
You should probably add some error checking, etc, but this is probably the easiest way to do it:
System.Uri address = new System.Uri("http://tinyurl.com/api-create.php?url=" + YOUR ADDRESS GOES HERE);
System.Net.WebClient client = new System.Net.WebClient();
string tinyUrl = client.DownloadString(address);
Console.WriteLine(tinyUrl);
After doing some more research ... I stumbled upon the following code:
public static string MakeTinyUrl(string url)
{
try
{
if (url.Length <= 30)
{
return url;
}
if (!url.ToLower().StartsWith("http") && !Url.ToLower().StartsWith("ftp"))
{
url = "http://" + url;
}
var request = WebRequest.Create("http://tinyurl.com/api-create.php?url=" + url);
var res = request.GetResponse();
string text;
using (var reader = new StreamReader(res.GetResponseStream()))
{
text = reader.ReadToEnd();
}
return text;
}
catch (Exception)
{
return url;
}
}
Looks like it may do the trick.
Keep in mind if you're doing a full-scale app, that you're wiring in a pretty specific dependency to TinyURL's URL/API scheme. Maybe they have guarantees about their URL not changing, but it's worth checking out
You have to call that URL from your code, then read back the output from the server and process it.
Have a look at the System.Net.WebClient class, DownloadString (or better: DownloadStringAsync) seems to be what you want.
According to this article, you could implement it like this:
public class TinyUrlController : ControllerBase
{
Dictionary dicShortLohgUrls = new Dictionary();
private readonly IMemoryCache memoryCache;
public TinyUrlController(IMemoryCache memoryCache)
{
this.memoryCache = memoryCache;
}
[HttpGet("short/{url}")]
public string GetShortUrl(string url)
{
using (MD5 md5Hash = MD5.Create())
{
string shortUrl = UrlHelper.GetMd5Hash(md5Hash, url);
shortUrl = shortUrl.Replace('/', '-').Replace('+', '_').Substring(0, 6);
Console.WriteLine("The MD5 hash of " + url + " is: " + shortUrl + ".");
var cacheEntryOptions = new MemoryCacheEntryOptions().SetSlidingExpiration(TimeSpan.FromSeconds(604800));
memoryCache.Set(shortUrl, url, cacheEntryOptions);
return shortUrl;
}
}
[HttpGet("long/{url}")]
public string GetLongUrl(string url)
{
if (memoryCache.TryGetValue(url, out string longUrl))
{
return longUrl;
}
return url;
}
}
Here my version of the implementation:
static void Main()
{
var tinyUrl = MakeTinyUrl("https://stackoverflow.com/questions/366115/using-tinyurl-com-in-a-net-application-possible");
Console.WriteLine(tinyUrl);
Console.ReadLine();
}
public static string MakeTinyUrl(string url)
{
string tinyUrl = url;
string api = " the api's url goes here ";
try
{
var request = WebRequest.Create(api + url);
var res = request.GetResponse();
using (var reader = new StreamReader(res.GetResponseStream()))
{
tinyUrl = reader.ReadToEnd();
}
}
catch (Exception exp)
{
Console.WriteLine(exp);
}
return tinyUrl;
}
来源:https://stackoverflow.com/questions/366115/using-tinyurl-com-in-a-net-application-possible