Please help: console connect to a webpage

Hi, I've been trying and failing at this for hours and I've come to a point of desperate frustration. I am trying to write a console app that connects to any given website, say MSN.com, downloads and then displays the HTML code for that site. Can anyone please explain to me, step-by-step, how to do this (preferably with code examples). Thanks, it would be greatly appreciated.



Answer this question

Please help: console connect to a webpage

  • Roto

    try thr code I sent you. it takes response from www.live.com and it do not use your IIS

    try it before change anything to make sure it works and you understood the code


  • Philippe Cand

    using System.Net;

    NOTE: Remove line with proxy class if you do not have proxy server


  • LotusExigeS1

    Hi. The following code connects to webpage and display result in console


    WebClient client = new WebClient();
    client.Proxy = new WebProxy("192.168.0.1", 8080);

    Stream data = client.OpenRead("http://www.live.com");
    StreamReader reader = new StreamReader(data);
    string s = reader.ReadToEnd();

    Console.WriteLine(s);

    data.Close();
    reader.Close();



    For more info take a look at WebClient class

    hope this helps


  • Redsome

    What libraries should I use I'll check out that link of yours while I wait. Thanks a million mate.

  • double digger

    One more problem, the console isn't displaying anything. I believe this has something to do with my IIS setup, is this possible

  • ARme

    Right guys, I got it working. I'm all smiles now. Mark, Galin, thanks again.

  • talismax

    Nice idea for an application, would be great with some syntax highlighting. Don't forget to leave credit to the members thats gave you assistance.


  • YA say hello

    This should work:

    using System;

    using System.Collections.Generic;

    using System.Text;

    using System.Net;

    using System.IO;

    namespace WebRequestConsole

    {

    class Program

    {

    static void Main(string[] args)

    {

    try

    {

    // HttpWebRequest not created directly, uses WebRequest factory method

    HttpWebRequest request = WebRequest.Create("http://www.msn.com/") as HttpWebRequest;

    if(request != null)

    {

    // Cast response to HttpWebResponse

    using(HttpWebResponse response = request.GetResponse() as HttpWebResponse)

    {

    Stream stream = response.GetResponseStream();

    // Read characters from Stream 1K at a time

    using(StreamReader reader = new StreamReader(stream))

    {

    char[] buffer = new char[1024];

    int count;

    while((count = reader.Read(buffer, 0, 1024)) > 0)

    {

    Console.Write(new string(buffer, 0, count));

    }

    reader.Close();

    }

    response.Close();

    }

    }

    }

    catch(Exception ex)

    {

    Console.Error.WriteLine(ex.ToString());

    }

    Console.ReadLine();

    }

    }

    }



  • Please help: console connect to a webpage