public static async Task<HtmlDocument> Web_Get_HtmlDocument(string sURL)
{
//------------< Web_Get_HtmlDocument() >------------
//* get the HTML Document of a website-URL
try
{
//----< Read HTML from Website >----
//< read HTML >
string sHTML = ""; //Client Request as string
try
{
string sDownloadType = "webrequest";
if (sDownloadType=="webrequest")
{
//--< download as webrequest >--
WebRequest objRequest = WebRequest.Create(sURL);
objRequest.CachePolicy = new HttpRequestCachePolicy(HttpRequestCacheLevel.NoCacheNoStore);
HttpWebResponse objResponse = (HttpWebResponse)objRequest.GetResponse();
//</ WebRequest and Response >
//< Stream and Reader >
Stream objDataStream = objResponse.GetResponseStream();
StreamReader TextReader = new StreamReader(objDataStream);
sHTML = TextReader.ReadToEnd();
//</ Stream and Reader >
//--</ download as webrequest >--
}
else
{
//--< download as string >--
//-< HttpClient >-
HttpBaseProtocolFilter filter = new HttpBaseProtocolFilter();
filter.CacheControl.ReadBehavior = HttpCacheReadBehavior.NoCache;
filter.CacheControl.WriteBehavior = HttpCacheWriteBehavior.Default;
filter.CookieUsageBehavior = HttpCookieUsageBehavior.Default;
Windows.Web.Http.HttpClient client = new Windows.Web.Http.HttpClient(filter);
//-</ HttpClient >-
sHTML = await client.GetStringAsync(new Uri(sURL));
//--</ download as string >--
}
}
catch (Exception ex)
{
//clsSys.show_Message(ex.Message);
clsSys.fx_Log("Error httpClient: " + ex.Message);
return null;
}
//</ read HTML >
//----</ Read HTML from Website >----
clsSys.fx_Log("read HTML=" + sHTML.Substring(0, 10) + "..");
//< get HTMLdocument >
//*create and load to local HtmlDocument
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(sHTML);
//</ get HTMLdocument >
//< output >
return doc;
//</ output >
}
catch (Exception ex)
{
clsSys.fx_Log("ERROR get HtmlDocument URL=" + sURL + " Msg:" + ex.Message);
return null;
}
//------------</ Web_Get_HtmlDocument() >------------
}
|