使用C#从短网址中获取长网址
作者:互联网
我一直在使用LongURL.org API扩展短网址.此服务的优点在于,它返回一个长URL,实际页面的标题和元信息.
我真正的问题是,获取数据似乎花费了过多的时间.我正在考虑将请求转移到JavaScript,以便通过AJAX更新面板获取URL,以便快速加载页面,并在用户查看内容(某些搜索结果)时更新URL数据.
有谁知道我还能在更好的时间范围内收集上述信息吗?我正在使用C#ASP.NET,但会考虑其他语言的解决方案.对此领域的任何指导均深表感谢.
解决方法:
这是我之前在一个项目中使用过的一个…
private string UrlLengthen(string url)
{
string newurl = url;
bool redirecting = true;
while (redirecting)
{
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(newurl);
request.AllowAutoRedirect = false;
request.UserAgent = "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 4.0.20506)";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
if ((int)response.StatusCode == 301 || (int)response.StatusCode == 302)
{
string uriString = response.Headers["Location"];
Log.Debug("Redirecting " + newurl + " to " + uriString + " because " + response.StatusCode);
newurl = uriString;
// and keep going
}
else
{
Log.Debug("Not redirecting " + url + " because " + response.StatusCode);
redirecting = false;
}
}
catch (Exception ex)
{
ex.Data.Add("url", newurl);
Exceptions.ExceptionRecord.ReportWarning(ex); // change this to your own
redirecting = false;
}
}
return newurl;
}
标签:url,api,expander,asp-net,c 来源: https://codeday.me/bug/20191024/1917015.html