111 lines
3.0 KiB
C#
111 lines
3.0 KiB
C#
using System;
|
|
using System.IO;
|
|
using System.Text;
|
|
using System.Xml;
|
|
using HtmlAgilityPack;
|
|
|
|
namespace Endpoint
|
|
{
|
|
public class HtmlEndpoint : HttpEndpoint
|
|
{
|
|
/// <summary>
|
|
/// Returns the HTML parsed into a standard XmlDocument.
|
|
/// Uses the HtmlAgilityPack library for "out of the web" (poorly formatted) html file support.
|
|
/// </summary>
|
|
/// <param name="html">The HTML</param>
|
|
/// <returns>An XmlDocument from the HTML</returns>
|
|
private static XmlDocument GetHtmlXml(string html)
|
|
{
|
|
var htmlDocument = new HtmlDocument();
|
|
htmlDocument.LoadHtml(html);
|
|
|
|
var xmlDocument = new XmlDocument();
|
|
using (Stream stream = new MemoryStream())
|
|
{
|
|
XmlWriter xmlTextWriter = new XmlTextWriter(stream, Encoding.UTF8);
|
|
|
|
htmlDocument.Save(xmlTextWriter);
|
|
|
|
stream.Seek(0, SeekOrigin.Begin);
|
|
|
|
using (var sr = new StreamReader(stream))
|
|
xmlDocument.Load(sr);
|
|
|
|
xmlTextWriter.Close();
|
|
}
|
|
return xmlDocument;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the HTML grabbed from the passed in URL parsed into a standard XmlDocument.
|
|
/// Uses the HtmlAgilityPack library for "out of the web" html file support.
|
|
/// </summary>
|
|
/// <param name="url">The URL.</param>
|
|
/// <param name="requestContent">The post data.</param>
|
|
/// <returns>An XmlDocument from the HTML</returns>
|
|
private static XmlDocument GetHtmlXml(Uri url, string requestContent)
|
|
{
|
|
return GetHtmlXml(GetUrlContent(url, "application/x-www-form-urlencoded", requestContent, null));
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// Gets the HTTP endpoint configuration.
|
|
/// </summary>
|
|
/// <value>The HTTP endpoint configuration.</value>
|
|
public HtmlEndpointConfiguration HtmlEndpointConfiguration
|
|
{
|
|
get { return (HtmlEndpointConfiguration)_httpEndpointConfiguration; }
|
|
}
|
|
|
|
/// <summary>
|
|
/// Initializes a new instance of the <see cref="HttpEndpoint"/> class.
|
|
/// </summary>
|
|
/// <param name="config">The HttpEndpointConfiguration.</param>
|
|
public HtmlEndpoint(HtmlEndpointConfiguration config)
|
|
: base(config)
|
|
{
|
|
}
|
|
|
|
/// <summary>
|
|
/// Gets the current status result of the endpoint
|
|
/// </summary>
|
|
/// <returns>Status</returns>
|
|
public override Status GetStatus()
|
|
{
|
|
Status baseStatus = base.GetStatus();
|
|
if (baseStatus != Status.Up)
|
|
return baseStatus;
|
|
|
|
try
|
|
{
|
|
XmlDocument xml = GetHtmlXml(HttpEndpointConfiguration.Uri, HtmlEndpointConfiguration.RequestContent);
|
|
|
|
// xml.Save(@"c:\test.xml");
|
|
|
|
XmlNodeList nodes = xml.SelectNodes(HtmlEndpointConfiguration.XpathQuery);
|
|
|
|
// verify html has expected value
|
|
if (nodes == null || nodes.Count == 0)
|
|
{
|
|
StatusDescription = "Couldn't find expected value in html";
|
|
return Status.Error;
|
|
}
|
|
string value = nodes[0].Value.Trim();
|
|
if (value != HtmlEndpointConfiguration.ExpectedXpathResult)
|
|
{
|
|
StatusDescription = String.Format("Result was: '{0}', was expecting '{1}'", value,
|
|
HtmlEndpointConfiguration.ExpectedXpathResult);
|
|
return Status.Error;
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
StatusDescription = ex.Message;
|
|
return Status.Error;
|
|
}
|
|
return Status.Up;
|
|
}
|
|
}
|
|
}
|