来自WebResponse的HtmlDocument.LoadHtml?

.net c# html html-agility-pack httpwebrequest

我尝试从发布的链接中提取图像,我做的第一个检查是查看链接是否是这样的普通图像:

    HttpWebRequest request;
    WebResponse webresponse;

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

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

如果没有找到,我想继续使用HTML Agility Pack但是为了能够做到这一点,我需要运行:

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

问题是即使我确定响应中有HTML代码,LoadHtml也找不到任何源代码。我怀疑HTML的格式不正确?

以下是ReadToEnd将生成的内容的一部分:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="sv" lang="sv">
    <head><title>
        X - Eclipse - 2011
    </title>

        <!--[if lt IE 7]>
        <script defer type="text/javascript" src="../javascript/pngfix.js"></script>
        <![endif]-->
        <!--<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="X.net" /><meta name="description" content="Välkommen in till ett av Sveriges största Xcommunity." /><meta name="keywords" content="X, rollspel, boardgamegeek, boardgame, X.net, X.net, 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>
    <body>
        <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....

我可以看到该字符串包含一些新行(\ r \ n)命令,如果这很重要?

我的目标很简单,一次避免下载网页,否则我可以使用WebClient.DownloadString(url);以我认识的格式下载它。

一般承认的答案

这工作:

request = (HttpWebRequest)HttpWebRequest.Create(url);
webresponse = (HttpWebResponse)request.GetResponse();
if (webresponse.ContentType.StartsWith("image/"))
{...}
if (webresponse.ContentType.StartsWith("text/html"))
{
     var resultStream = webresponse.GetResponseStream();
     doc.Load(resultStream);
}



许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因
许可下: CC-BY-SA with attribution
不隶属于 Stack Overflow
这个KB合法吗? 是的,了解原因