1

i'm writing a snippet of code to get the source code of an html page from a website but the variable sourceCode remains null and it does not get the html code

this is my code class HtmlClass { public static string getSourceCode(string url) { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url); HttpWebResponse resp = (HttpWebResponse)req.GetResponse(); StreamReader sr = new StreamReader(resp.GetResponseStream()); string sourceCode = sr.ReadToEnd(); sr.Close(); resp.Close(); return sourceCode; } }

and this is where i use it: private void button3_Click(object sender, EventArgs e) { string url = textBox1.Text; string sourceCode = HtmlClass.getSourceCode(url); }

can you please tell me what might be wrong???

raza
  • 31
  • 4

2 Answers2

0

Maybe your URL is null?

An easier way to do it:

using System.Net;
using System.Net.Http;  // in LINQPad, also add a reference to System.Net.Http.dll

WebRequest req = HttpWebRequest.Create("http://google.com");
req.Method = "GET";

string source;
using (StreamReader reader = new StreamReader(req.GetResponse().GetResponseStream()))
{
    source = reader.ReadToEnd();
}

Console.WriteLine(source);

From:

How can I download HTML source in C#

Community
  • 1
  • 1
Destrif
  • 2,104
  • 1
  • 14
  • 22
0

If you are working with c# for scraping use HtmlAgilityPack nuget package or you can also download it's dll from internet this is the easiest way to done the scraping using c#.

HtmlWeb htmlWeb = new HtmlWeb();
HtmlDocument htmlDocument = htmlWeb.Load("http://google.com");

then you perform all your required operation on htmldocument easily. Refer below link for the same. C# web Scraping

Nikhil.Patel
  • 959
  • 9
  • 17