HtmlDocument.LoadHtml from WebResponse?

.net c# html html-agility-pack httpwebrequest


I try to extract image from a posted link and the first check I do is to see if the link is to a plain image like this :

    HttpWebRequest request;
    WebResponse webresponse;

    request = (HttpWebRequest)HttpWebRequest.Create(url);

     webresponse = request.GetResponse();
     if (webresponse.ContentType.StartsWith("image/"))

If this is not found I want to go on with the HTML Agility Pack but to be able to do that I need to run :

HtmlDocument doc;
reader = new StreamReader(webresponse.GetResponseStream());

The problem is that LoadHtml will not find any source even when Im sure that there is HTML code in the response. I suspect that the formation of the HTML is not in correct format?

Here is part of what the ReadToEnd will generate :

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

    <html xmlns="" xml:lang="sv" lang="sv">
        X - Eclipse - 2011

        <!--[if lt IE 7]>
        <script defer type="text/javascript" src="../javascript/pngfix.js"></script>
        <!--<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />-->

        <meta http-equiv="Content-type" content="text/html; charset=iso-8859-1" /><link href="../../../App_Themes/X/mainStyleSheet.css" type="text/css" rel="stylesheet" /><meta name="author" content="" /><meta name="copyright" content="" /><meta name="description" content="Välkommen in till ett av Sveriges största Xcommunity." /><meta name="keywords" content="X, rollspel, boardgamegeek, boardgame,,, community, Jimmy, Nilsson, schack, risk, puerto rico" /><script language="javascript" type="text/javascript" src="/sites/X/javascript/common.js"></script><script language="javascript" type="text/javascript" src="/sites/X/javascript/ajaxHandler.js"></script><script language="javascript" type="text/javascript" src="/javascript/jquery.js"></script><link rel="shortcut icon" href="/App_Themes/X/Images/common/browserIcon/favicon.ico" /><link rel="icon" href="/App_Themes/X/Images/common/browserIcon/animated_favicon1.gif" type="image/gif" /></head>
        <div id="topBack">
        <div id="siteContainer">
        <form method="post" action="game.aspx?gameId=72125" id="aspnetForm" enctype="multipart/form-data">

<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDw....

I can see that the string contains some newline(\r\n) commands if that matters?

My goal is simple to avoid downloading the webpage more then one time, else I could use the WebClient.DownloadString(url); to download it in a format that I know works.

3/22/2013 11:22:30 AM

Accepted Answer

This worked :

request = (HttpWebRequest)HttpWebRequest.Create(url);
webresponse = (HttpWebResponse)request.GetResponse();
if (webresponse.ContentType.StartsWith("image/"))
if (webresponse.ContentType.StartsWith("text/html"))
     var resultStream = webresponse.GetResponseStream();
3/22/2013 6:05:02 PM

Related Questions


Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow