How to read HTML source of a from URL in C#

2 Answers

0 votes
using System;
using System.IO;
using System.Net;
using System.Text;

namespace ConsoleApplication_C_Sharp
{
    static class Program
    {
        static void Main(string[] args)
        {
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create("http://www.collectivesolver.com/");
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            if (response.StatusCode == HttpStatusCode.OK)
            {
                Stream receiveStream = response.GetResponseStream();
                StreamReader sr = null;

                if (response.CharacterSet == null)
                {
                    sr = new StreamReader(receiveStream);
                }
                else
                {
                    sr = new StreamReader(receiveStream, Encoding.GetEncoding(response.CharacterSet));
                }

                string htmlText = sr.ReadToEnd();

                Console.WriteLine(htmlText);

                response.Close();
                sr.Close();
            }
        }
    }
}


/*
run:
  
...unt-pad"> views</span>

        </span>

</div>

<div class="qa-q-item-main">

        <div class="qa-q-item-title">

                <a href="./12057/how-to-extract-only-words-with-first-letter-low
ercase-from-a-string-in-javascript">How to extract only words with first-letter
lowercase from a string in JavaScript</a>

...

   
*/

 



answered Feb 8, 2017 by avibootz
0 votes
using System;
using System.Net;

namespace ConsoleApplication_C_Sharp
{
    static class Program
    {
        static void Main(string[] args)
        {
            using (WebClient client = new WebClient())
            {
                string html = client.DownloadString("http://seek4info.com/");

                Console.WriteLine(html);
            }
        }
    }
}


/*
run:
  
<!doctype html>
<html>
    <head>
        <title>Seek.4Info - Relevant Search Engine</title>        <meta http-equ
iv="Content-type" content="text/html; charset=utf-8">
        <meta name="keywords" content="Search, Query, Search Engine, Relevant, L
inks, Urls, Info, Find, Information">
        <meta name="description" content="Relevant Links &amp; Information On An
y Keyword">

...

*/

 



answered Feb 8, 2017 by avibootz

Related questions

1 answer 251 views
2 answers 367 views
1 answer 276 views
1 answer 211 views
1 answer 116 views
1 answer 8,696 views
8,696 views asked Mar 22, 2020 by avibootz
...