Wednesday, May 13, 2015

C# get web page content as string

Background

I want to write a scrapper application to get product price on a website, but first thing needed to do is to get the content of the web page. So how to get web page content using C#?

Solution

There are mainly two ways to do it: WebClient and WebRequest.
Using WebClient is a simpler method
WebClient client = new WebClient();
String content = client.DownloadString(url);

Using WebRequest is however a more complex way
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
WebResponse response = request.GetResponse();
String content = new StreamReader(response.GetResponseStream()).ReadToEnd();


Graphviz4Net The system cannot find the file specified

What is Graphviz4Net?

Graphviz4Net is a .NET port for Graphviz library. It provides controls to draw beautiful graph, such like curve edges, sub-graph, etc.

This is its homepage: http://graphviz4net.codeplex.com/


WPF Example

And there is a WPF sample program within its download package file. After opening the sample project, we can see the project structure:

The project is mainly divided into two parts: main window and ViewModel (a good architecture).
When I tried to run this sample, I got an exception text displayed on the main window: Graphviz4Net: an exception was thrown during layouting. Exception message: The system cannot find the file specified.

This problem is caused by that Graphviz4Net is dependent on the "dot" program which is part of Graphviz (http://graphviz.org). So we need download Graphviz first for our application.