I’m using this code to change the href attribute of a HTML stream.
first I download a full html page using this code:(URL is webpage address)
HttpWebRequest myHttpWebRequest = (HttpWebRequest)WebRequest.Create(URL);
HttpWebResponse myHttpWebResponse =
(HttpWebResponse)myHttpWebRequest.GetResponse();
Stream s = myHttpWebResponse.GetResponseStream();
then I process this:
HtmlDocument doc = new HtmlDocument();
doc.Load(s);
foreach (HtmlNode link in doc.DocumentNode.SelectNodes("/a"))
{
string att = link.Attributes["href"].Value;
link.Attributes["href"].Value = "http://ahmadalli.somee.com/default.aspx?url=" + att;
}
doc.Save(s);
s is html stream.
but I’ve got an exception that says doc.DocumentNode is null!
i tried many sites but doc.DocumentNode is null to
This works for me.
Also see the
HttpUtility.UrlEncodeto be able to get the url back correctly. Otherwise, some parameters in original url may cause problem.Use
HttpUtility.UrlDecodeto decode it.