Freelance Project Requests info@CodeDocu.de Software Development in C# WPF Asp.Net Core Vba Excel Word SQL-Server EF Linq, UWP Net
#

C#: Read a Web page asynchronously with WebClient
 
You can evaluate a website using c# by creating a so-called Web client, enter a Web address and then downloads the page as text.
Then, you can convert the text into an XML structure and evaluate each node.
 
This sample demonstrates reading the Web page with an asynchronous download of the text. 
It will be best to prevent the program of hanging in an wait loop for the result by running the process of download in an asynchronous operation.
 
An asychrone version means for the Web client, that the event webClient.DownloadStringCompleted has to be defined previously and then starts the download.

//< get Website >
//*here: download as string
webClient.DownloadStringCompleted += WebClient_DownloadStringCompleted;
webClient.DownloadStringAsync(address);
//</ get Website >
 

 
 

 
To start the download, the program first then comes back when the download is finished.
Meanwhile, you can continue to run other processes.

private void WebClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
//-------------------< WebClient_DownloadStringCompleted() >-------------------
..Download completed
..code..
//-------------------</ WebClient_DownloadStringCompleted() >-------------------
}

 
After downloading, if you load the result in an HTML structure with the method. LoadHtml(string).
"DownloadString" means that the result of the Web client itself is a string.

var document = new HtmlDocument();
document.LoadHtml(e.Result);
 

 
The HTMLDocument thus obtained can then be evaluated after node and HTMLElementen.

var node = document.DocumentNode.SelectSingleNode("//span[@id=\"result_box\"]");
var output = node != null ? node.InnerText : e.Error.Message;
 

 

 
Video tutorial

 
Subject: WPF, WinForms, UWP(abgeändert), HTMLDocument, HTMLAgilityPack
 
 
Complete code in c# to use:

private void get_Document_Webclient_async(string url)
{
//-------------------< get_Document_Webclient_async() >-------------------
//*url like: https://translate.google.com/?hl=en#de/en/Hallo
//result is in the event: WebClient_DownloadStringCompleted
 
Uri address = new Uri(url);
 
WebClient webClient = new WebClient();
webClient.Headers[HttpRequestHeader.ContentType] = "application/x-www-form-urlencoded";
 
//< get Website >
//*here: download as string
webClient.DownloadStringCompleted += WebClient_DownloadStringCompleted;
webClient.DownloadStringAsync(address);
//</ get Website >
 
//----</ HTMLDocument >----
 
//-------------------</ get_Document_Webclient_async() >-------------------
}
 
 
private void WebClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
//-------------------< WebClient_DownloadStringCompleted() >-------------------
//< check >
if (e.Error != null)
{
var output = e.Error.Message;
}
//</ check >
else if (e.Result != null)
{
var document = new HtmlDocument();
document.LoadHtml(e.Result);
 
var node = document.DocumentNode.SelectSingleNode("//span[@id=\"result_box\"]");
var output = node != null ? node.InnerText : e.Error.Message;
 
}
//-------------------</ WebClient_DownloadStringCompleted() >-------------------
}

 
 
 
Mobile
»
C#: Read a Web page asynchronously with WebClient
»
C#: read Web page as HTMLDocument

.

Contact for Jobs, Project Requests: raimund.popp@microsoft-programmierer.de