使用 C# 将短 URL 转为长 URL

发布于 2024-09-08 14:29:29 字数 324 浏览 5 评论 0原文

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

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

有谁知道我还能如何在更好的时间内收集上述信息?我正在使用 C# ASP.NET,但会考虑其他语言的解决方案。非常感谢该领域的任何指导。

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.

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).

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

谈下烟灰 2024-09-15 14:29:29

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

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

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文