C# htmlagility pack, capturing redirct

倾然丶 夕夏残阳落幕 提交于 2019-12-07 22:26:11

问题


HI all, this one is really simple (I hope). I'm using htmlagility pack to do my webcrawling. So what happens if I input url whatever, that then directs me to a new url, how do I capture that new redirected URL?

If htmlagility pack doesnt have a way, can someone suggest another method?


回答1:


When you create your HttpWebRequest you can set AllowAutoRedirect property to true and it will automatically follow any redirects you have.

HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create("http://www.contoso.com");  
myHttpWebRequest.MaximumAutomaticRedirections=1;
myHttpWebRequest.AllowAutoRedirect=true;
HttpWebResponse myHttpWebResponse=(HttpWebResponse)myHttpWebRequest.GetResponse(); 

you can find more info at msdn




回答2:


Using the HtmlWeb class that comes with the Html Agility Pack, you can tweak the request before it's actually executed, like this:

    HtmlWeb web = new HtmlWeb();
    web.PreRequest = OnPreRequest;
    HtmlDocument doc = web.Load("http://wwwblablahh.com");


private static bool OnPreRequest(HttpWebRequest request)
{
    request.AllowAutoRedirect = true;
    return true;
}


来源:https://stackoverflow.com/questions/6239319/c-sharp-htmlagility-pack-capturing-redirct

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!