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();


No comments:

Post a Comment