一种找出重定向 URL 的方法 [英] A way to figure out redirection URL

查看:18
本文介绍了一种找出重定向 URL 的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给定一个重定向到第 3 方网站 B 的 URL A,在我的应用程序中,我需要找出给定 url A 的 URL B 并将其插入数据库,这可以是 Windows 应用程序或 Web 或任何方式使用 C# 更快更容易!谢谢!

Given a URL A which gets redirected to a 3rd party website B, in my application I need to to find out URL B for the given url A and insert it in DB , this can be a windows application or web or whichever way is faster and easier using C#! Thanks !

附言我不需要代码插入数据库.

P.S. I do not require the code to insert in DB.

推荐答案

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

WebRequest follows redirects without user intervention, so if the redirects are using 301/302 status codes then the following will work

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

如果重定向是使用 javascript 或 HTTP-Equiv 元标记创建的,那么您必须解析页面并查找这些.HTML 敏捷包可能是做到这一点的最佳方式.

If the redirects are created using javascript or HTTP-Equiv meta tags then you're doing to have to parse the page and look for those. The HTML agility pack is probably the best way to do this.

为了更进一步,下面是一个类,它将手动解析主要的 HTTP 重定向状态代码,在它发生时建立历史

To take this a little further the following is a class which will manually resolve the main HTTP redirect status codes, building up a history as it goes

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

这篇关于一种找出重定向 URL 的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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