使用 C# 的短网址中的长网址 [英] Long URLs from short ones using C#

查看:26
本文介绍了使用 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天全站免登陆