我想在MVC4中使用Tiny-URL API,任何想法如何在我的解决方案中使用该API?
我审阅了它的文档,但它是在PHP文档link
中答案 0 :(得分:0)
你可以use same code as in this answer,但使用不同的uri。
首先,您需要请求API密钥并相应地设置apikey
变量。然后选择您将使用的提供商字符串from API docs(我在下面的示例中使用0_mk
0.mk
提供商。)
然后你可以撰写网址并提出这样的请求:
string yourUrl = "http://your-site.com/your-url-for-minification";
string apikey = "YOUR-API-KEY-GOES-HERE";
string provider = "0_mk"; // see provider strings list in API docs
string uriString = string.Format(
"http://tiny-url.info/api/v1/create?url={0}&apikey={1}&provider={2}&format=text",
yourUrl, apikey, provider);
System.Uri address = new System.Uri(uriString);
System.Net.WebClient client = new System.Net.WebClient();
try
{
string tinyUrl = client.DownloadString(address);
Console.WriteLine(tinyUrl);
}
catch (Exception ex)
{
Console.WriteLine("network error occurred: {0}", ex);
}
根据文档,默认格式为format=text
,因此您无需指定它。如果需要,您还可以使用format=xml
或format=json
,但是您需要解析输出(并且您将有state
字段作为响应,并且可能会处理错误)。
更新:使用.NET 4.5异步获取小网址等待关键字可以与WebClient.DownloadStringAsync()
功能一起使用(您应该在功能中执行此操作,标记为 async 关键字):
...
string tinyUrl = await client.DownloadStringAsync(uriString);
...