一种计算重定向URL的方法

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

PS我不需要在DB中插入代码。

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

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

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

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

 ///  /// Digs through HTTP redirects until a non-redirected URL is found. ///  public class Digger { ///  /// Initializes a new instance of the  class. ///  public Digger() : this(20) { } ///  /// Initializes a new instance of the  class. ///  /// The maximum depth of redirects to parse. public Digger(int maximumDepth) { this.MaximumDepth = maximumDepth; } ///  /// Gets the maximum depth of redirects to parse. ///  /// The maximum depth of redirects to parse. public int MaximumDepth { get; private set; } ///  /// Resolves any redirects at the specified URI. ///  /// The initial URI. /// The URI after resolving any HTTP redirects. public Uri Resolve(Uri destination) { List redirectHistory = new List(); return this.Resolve(destination, redirectHistory); } ///  /// Resolves any redirects at the specified URI. ///  /// The initial URI. /// A collection of  objects representing the redirect history. /// The URI after resolving any HTTP redirects. public Uri Resolve(Uri destination, ICollection redirectHistory) { redirectHistory.Add(destination); return this.Resolve(destination, this.MaximumDepth, redirectHistory); } ///  /// Resolves any redirects at the specified URI. ///  /// The initial URI. /// The maximum number of redirects left to follow. /// A collection of  objects representing the redirect history. /// The URI after resolving any HTTP redirects. private Uri Resolve(Uri destination, int hopsLeft, ICollection 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; } } 
 Uri MyUrl = Request.UrlReferrer; Response.Write("Referrer URL Port: " + Server.HtmlEncode(MyUrl.Port.ToString()) + "
"); Response.Write("Referrer URL Protocol: " + Server.HtmlEncode(MyUrl.Scheme) + "
");

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

我假设您知道如何使用LINQ在db中保存记录。 如果您不这样做,请点击此链接: LINQ to SQL – 5分钟概述

希望能帮助到你。