WebRequest folgt Umleitungen ohne Eingreifen des Benutzers, so dass, wenn die Umleitungen verwenden 301/302 Statuscodes dann wird die folgende
WebRequest request = WebRequest.Create(destination);
WebResponse response = request.GetResponse();
Console.WriteLine(response.ResponseUri);
funktionieren, wenn die Umleitungen erstellt werden, mit Hilfe von Javascript oder http-equiv Meta-Tags Sie dann‘ Sie müssen die Seite analysieren und nach diesen suchen. Das HTML Agility Pack ist wahrscheinlich der beste Weg, dies zu tun.
dies ein wenig weiter gehen folgende ist eine Klasse, die die Haupt-HTTP-Umleitung Statuscodes manuell lösen wird, eine Geschichte aufzubauen, wie es
/// <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;
}
}
geht davon abhängt, wie die Umleitung funktioniert. Serverseitig? Client-Seite (d.h. JS)? Versuchen Sie, die URL mit den http-Wrappern, die C# hat, abzurufen, und folgen Sie allen 301s/302s. Wenn du Glück hast, gibt es vielleicht sogar eine Bibliothek, die das für dich macht. Was passiert, wenn B zu C umleitet? Möchten Sie B oder C speichern? Wie weit werden Sie Weiterleitungen folgen? Was passiert, wenn C nach B umleitet? Stellen Sie sicher, dass Sie unendliche Umleitungsschleifen vermeiden, indem Sie verfolgen, welche Adressen Sie besucht haben, oder ein Umleitungslimit festlegen (was meiner Meinung nach ist, wie Firefox/Chrome mit diesem Problem umgeht). –
Server-seitiges Beispiel wäre in Ordnung ... Dies ist nur ein Werkzeug, um Daten (d. H. Endgültige URL) zu extrahieren, so muss nicht ausgefallen sein ... kann in irgendeiner Weise getan werden! "A" wird immer auf "B" umgeleitet und es gibt keine weitere Weiterleitung von dort, die eine feststehende Tatsache ist. –
Client-Seite Code tut auch nicht weh ... Ich nehme eine Win-Form-Anwendung mit einer Instanz von IE innerhalb sollte den Job tun ... einfach nicht sicher –