一种计算重定向URL的方法

时间:2009-09-05 06:41:25

标签: c# url redirect

鉴于URL A被重定向到第三方网站B,在我的应用程序中,我需要找到给定URL A的URL B并将其插入DB,这可以是Windows应用程序或Web或以任何方式使用C#更快更容易! 谢谢!

P.S。我不需要在DB中插入代码。

2 个答案:

答案 0 :(得分:11)

WebRequest遵循重定向而无需用户干预,因此如果重定向使用301/302状态代码,则以下内容将起作用

WebRequest request = WebRequest.Create(destination);
WebResponse response = request.GetResponse();
Console.WriteLine(response.ResponseUri);

如果使用javascript或HTTP-Equiv元标记创建重定向,那么您必须解析页面并查找它们。 HTML敏捷包可能是执行此操作的最佳方式。

为了更进一步,以下是一个类,它将手动解析主HTTP重定向状态代码,建立历史记录

/// <summary>
/// Digs through HTTP redirects until a non-redirected URL is found.
/// </summary>
public class Digger
{
    /// <summary>
    /// Initializes a new instance of the <see cref="Digger"/> class.
    /// </summary>
    public Digger() : this(20)
    {            
    }

    /// <summary>
    /// Initializes a new instance of the <see cref="Digger"/> class.
    /// </summary>
    /// <param name="maximumDepth">The maximum depth of redirects to parse.</param>
    public Digger(int maximumDepth)
    {
        this.MaximumDepth = maximumDepth;
    }

    /// <summary>
    /// Gets the maximum depth of redirects to parse.
    /// </summary>
    /// <value>The maximum depth of redirects to parse.</value>
    public int MaximumDepth
    {
        get; 
        private set;
    }

    /// <summary>
    /// Resolves any redirects at the specified URI.
    /// </summary>
    /// <param name="destination">The initial URI.</param>
    /// <returns>The URI after resolving any HTTP redirects.</returns>
    public Uri Resolve(Uri destination)
    {
        List<Uri> redirectHistory = new List<Uri>();
        return this.Resolve(destination, redirectHistory);
    }

    /// <summary>
    /// Resolves any redirects at the specified URI.
    /// </summary>
    /// <param name="destination">The initial URI.</param>
    /// <param name="redirectHistory">A collection of <see cref="Uri"/> objects representing the redirect history.</param>
    /// <returns>The URI after resolving any HTTP redirects.</returns>
    public Uri Resolve(Uri destination, ICollection<Uri> redirectHistory)
    {
        redirectHistory.Add(destination);
        return this.Resolve(destination, this.MaximumDepth, redirectHistory);
    }

    /// <summary>
    /// Resolves any redirects at the specified URI.
    /// </summary>
    /// <param name="destination">The initial URI.</param>
    /// <param name="hopsLeft">The maximum number of redirects left to follow.</param>
    /// <param name="redirectHistory">A collection of <see cref="Uri"/> objects representing the redirect history.</param>
    /// <returns>The URI after resolving any HTTP redirects.</returns>
    private Uri Resolve(Uri destination, int hopsLeft, ICollection<Uri> redirectHistory)
    {
        HttpWebRequest request = (HttpWebRequest)WebRequest.Create(destination);
        request.AllowAutoRedirect = false;
        request.Method = "HEAD";

        HttpWebResponse response = (HttpWebResponse)request.GetResponse();

        Uri resolvedUri;

        if (response.StatusCode == HttpStatusCode.Redirect || 
            response.StatusCode == HttpStatusCode.Moved || 
            response.StatusCode == HttpStatusCode.MovedPermanently)
        {
            if (hopsLeft > 0)
            {
                Uri redirectUri = new Uri(response.GetResponseHeader("Location"));
                if (redirectHistory.Contains(redirectUri))
                {
                    throw new Exception("Recursive redirection found");
                }

                redirectHistory.Add(redirectUri);
                resolvedUri = this.Resolve(redirectUri, hopsLeft - 1, redirectHistory);
            }
            else
            {
                throw new Exception("Maximum redirect depth reached");
            }
        }
        else
        {
            resolvedUri = response.ResponseUri;
        }

        return resolvedUri;            
    }
}

答案 1 :(得分:0)

Uri MyUrl = Request.UrlReferrer;
Response.Write("Referrer URL Port: " + Server.HtmlEncode(MyUrl.Port.ToString()) + "<br>");
Response.Write("Referrer URL Protocol: " + Server.HtmlEncode(MyUrl.Scheme) + "<br>");

正如我从您的问题中所理解的那样,您可以使用这样的代码,这样您就可以使用Linq或其他ADO.NET方法查看以前的URL并将其保存到db中。

我假设您知道如何使用LINQ在db中保存记录。如果您不这样做,请点击以下链接:LINQ to SQL - 5 Minute Overview

希望它有所帮助。