Search code examples
c#html-agility-pack

HtmlAgilityPack - How to get url path after redirect


I'm trying to get a Url full path after it's doing redirect, simply here is the code:

 var documentx = new HtmlWeb().Load(textBox1.Text);

Where the textbox1.text value is "https://xxxx.org/file/download"

so after i run that code in real it's redirect and change the structure to:

https://xxxx.org/file/ur344333kd/45rrreew

so how i can get the new url path? using HtmlAgilityPack C# Winform. Thanks


Solution

  • By setting web.CaptureRedirect to true, and by querying web.ResponseUri,

    You can get the Url of the final request which actually downloaded the document:

    Note: I am sending this UserAgent string, just like my Chrome Browser because server behavior may change depending on it.

    HtmlWeb web = new HtmlWeb();
    
    web.UserAgent = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.149 Safari/537.36";
    
    web.CaptureRedirect = true;
    
    HtmlDocument doc = web.Load("http://www.google.com");
    
    Console.WriteLine("Response retrieved from: {0}", web.ResponseUri);
    

    The output is:

    Response retrieved from: https://www.google.com/?gws_rd=ssl