来自短网址的长网址(使用C#) [英] Long URLs from short ones using C#

查看:242
本文介绍了来自短网址的长网址(使用C#)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用 LongURL.org API来扩展短网址.此服务的妙处在于,它返回一个长URL,实际页面的标题和元信息.

I've been using the LongURL.org API for expanding short URLs. The great thing about this service is that it returns a long URL, the title of the actual page and meta-info.

我真正的问题是,获取数据似乎花费了过多的时间.我正在考虑将请求转移到JavaScript,以便通过AJAX更新面板获取URL,以便快速加载页面,并在用户查看内容(某些搜索结果)时更新URL数据.

The real problem I have is that it seems to take an inordinate amount of time to fetch the data. I'm considering shifting the request to JavaScript so that the URL is fetched via an AJAX update panel, in order that the page loads quickly, and the URL data is updated while the user looks at the content (some search results).

有人知道我还可以在更好的时间范围内收集上述信息吗?我正在使用C#ASP.NET,但会考虑其他语言的解决方案.非常感谢在此方面的任何指导.

Does anyone know how else I could gather the info described above, in a better time-frame? I'm using C# ASP.NET but would consider solutions in other languages. Any guidance in this area is much appreciated.

推荐答案

这是我之前在项目中使用过的一个...

Here's one I've used in a project before ...

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;
}

这篇关于来自短网址的长网址(使用C#)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆