< Request-Response Model & C++ .NET Examples | Main | Chap 7: DNS & IP Addressing >

 


 

Chapter 6 Part 8:

Introduction to System.Net

 

 

What do we have in this chapter 6 Part 8?

  1. C# Request and Response Program Example

  2. VB .NET Request and Response Program Example

  3. C++ Request and Response Web Client Program Example

  4. C# Request and Response Web Client Program Example

  5. VB .NET Request and Response Web Client Program Example

 

 

C# Request and Response Program Example

 

Create a new console application project. You can use the project and solution names as shown in the following Figure.

 

C# Request and Response Program Example - creating a new console application

 

Add the following code.

 

using System;

using System.IO;

using System.Net;

using System.Text;

 

namespace RequestResponseChap6CS

{

    /// <summary>

    /// Demonstrates the use of WebRequest and WebResponse by

    /// downloading the contents of a URL and displaying them on the console.

    /// </summary>

    class Program

    {

        [STAThread]

        static void Main(string[ ] args)

        {

            Console.WriteLine("Read content of http://wordpress.org/index.php");

            ResolveResource("http://wordpress.org/index.php");

            // If you want to test the local page, make sure the file is there...

            // in this case it is MyTestFile.txt

            Console.WriteLine("Read content of file://c:\\temp\\MyTestFile.txt");

            ResolveResource("file://c:\\temp\\MyTestFile.txt");

        }

 

        static void ResolveResource(string address)

        {

            try

            {

                // Create the request object

                WebRequest request = WebRequest.Create(address);

                Console.WriteLine("Create() is OK...");

 

                // Issue the request

                WebResponse response = request.GetResponse();

 

                // Read the content

                StreamReader reader = new StreamReader(response.GetResponseStream(), Encoding.ASCII);

                string content = reader.ReadToEnd();

                Console.WriteLine("ReadToEnd() is OK...");

 

                // Display the content to the console

                Console.WriteLine(content);

                Console.WriteLine("WriteLine() is OK...");

 

                // Close the response

                response.Close();

                Console.WriteLine("Close() is OK...");

            }

            catch (WebException wex)

            {

                // Display the exception details

                Console.WriteLine(wex.ToString());

            }

        }

    }

}

 

Build and run the project (Start Without Debugging). The following are sample outputs.

 

C# Request and Response Program Example - a sample output

 

C# Request and Response Program Example - a sample output, reading a local text file

 

VB .NET Request and Response Program Example

 

Create a new console application project. You can use the project and solution names as shown in the following Figure.

 

VB .NET Request and Response Program Example - a new console mode application project creation

 

Add the following code.

 

Imports System

Imports System.IO

Imports System.Net

Imports System.Text

 

' <summary>

' Demonstrates the use of WebRequest and WebResponse by

' downloading the contents of a URL and displaying them on the console.

' </summary>

Module Module1

 

    Sub Main()

        Console.WriteLine("Reading the http://www.wordpress.org/index.php")

        ResolveResource("http://www.wordpress.org/index.php")

        Console.WriteLine()

        ' Make sure the file is there, else exception will be thrown...

        Console.WriteLine("Reading the file://c:\\temp\\MyTestFile.txt")

        ResolveResource("file://c:\\temp\\MyTestFile.txt")

    End Sub

 

    Sub ResolveResource(ByVal address As String)

        Try

            ' Create the request object

            Dim request As WebRequest = WebRequest.Create(address)

            Console.WriteLine("Create() is OK...")

 

            ' Issue the request

            Dim response As WebResponse = request.GetResponse()

            Console.WriteLine("GetResponse() is OK...")

 

            ' Read the content

            Dim reader As New StreamReader(response.GetResponseStream(), Encoding.ASCII)

            Dim content As String = reader.ReadToEnd()

            Console.WriteLine("ReadToEnd() is OK...")

 

            ' Display the content to the console

            Console.WriteLine(content)

            Console.WriteLine("WriteLine() is OK...")

 

            ' Close the response

            response.Close()

            Console.WriteLine("Close() is OK...")

 

        Catch wex As WebException

            ' Display the exception details

            Console.WriteLine(wex.ToString())

        End Try

    End Sub

End Module

 

Build and run the project (Start Without Debugging). The following are sample outputs.

 

VB .NET Request and Response Program Example - a sample output with default argument

 

VB .NET Request and Response Program Example - a sample output reading a local text file

 

C++ Request and Response Web Client Program Example

 

Create a new CLR console application and you can use RequestResponseWebClientChap6CP as the project and solution names if you want.

 

C++ Request and Response Web Client Program Example - a new CLR console application project creation

 

Add the following code.

 

// RequestResponseWebClientChap6CP.cpp : main project file.

/// <summary>

/// This example demonstrates the use of WebClient by

/// downloading the contents of a URL and displaying them on the console.

/// </summary>

 

#include "stdafx.h"

 

using namespace System;

using namespace System::IO;

using namespace System::Net;

using namespace System::Text;

 

static void ResolveResource(String^ address)

{

    try

    {

        Console::WriteLine("Creating a WebClient()...");

        WebClient^ client = gcnew WebClient();

 

        // Download the content

        Console::WriteLine("Downloading the content into byte storage...");

        array<Byte>^ contentBytes = client->DownloadData(address);

        Console::WriteLine("DownloadData() is OK...");

 

        // Convert the content bytes to a string

        Console::WriteLine("Converting the content bytes to a string...");

        String^ content = Encoding::ASCII->GetString(contentBytes);

        Console::WriteLine("GetString() is OK...");

 

        // Display the content to the console

        Console::WriteLine("Displaying the content to the console...\n");

        Console::WriteLine(content);

        Console::WriteLine("WriteLine() is OK...");

    }

    catch (WebException^ wex)

    {

        // Display the exception details

        Console::WriteLine("Error: " + wex->Message);

    }

}

 

[STAThread]

int main(array<System::String ^> ^args)

{

    // Validating the input values

    if (args->Length < 1)

    {

        // Console::WriteLine("Usage: {0} URL", Environment::CommandLine);

        Console::WriteLine("Usage: Executable_file_name URI");

        Console::WriteLine("Example: Executable_file_name http://wordpress.org/index.php");

        // If you want to test the local page, make sure the file is there...

        Console::WriteLine("Example: Executable_file_name http://www.cuil.com/");

        // in this case it is file.htm

        Console::WriteLine("Example: Executable_file_name file://c:\\temp\\test.htm");

        // Return to OS

        return 0;

    }

 

    String^ URI = args[0];

    ResolveResource(URI);

    return 0;

}

 

Build and run the project. The following are the output samples.

 

C++ Request and Response Web Client Program Example - a sample output

 

C++ Request and Response Web Client Program Example - a sample output with argument

 

C++ Request and Response Web Client Program Example - another sample output with argument

 

The following output sample shows the output of reading a local file.

 

C++ Request and Response Web Client Program Example - a sample output reading a local html file  

 

C# Request and Response Web Client Program Example

 

Create a new console application project. You can use the project and solution name as shown in the following Figure.

 

C# Request and Response Web Client Program Example - a new console application project creation

 

Add the following code.

 

using System;

using System.IO;

using System.Net;

using System.Text;

 

namespace RequestResponseWebClientChap6CS

{

    /// <summary>

    /// Demonstrates the use of WebClient by

    /// downloading the contents of a URL and displaying them on the console.

    /// </summary>

    class Program

    {

        [STAThread]

        static void Main(string[ ] args)

        {

            ResolveResource("http://www.wordpress.org/index.php");

            Console.WriteLine();

            Console.WriteLine("Reading a local HTML file...");

            // Make sure the file is there to see the output, change the path & file accordingly...

            ResolveResource("file://c:\\temp\\test.htm");

        }

 

        static void ResolveResource(string address)

        {

            try

            {

                // Download the content

                WebClient client = new WebClient();

                byte[ ] contentBytes = client.DownloadData(address);

                Console.WriteLine("DownloadData() is OK...");

                // Convert the content bytes to a string

                string content = Encoding.ASCII.GetString(contentBytes);

                Console.WriteLine("GetString() is OK...");

                // Display the content to the console

                Console.WriteLine(content);

                Console.WriteLine("WriteLine() is OK...");

            }

            catch (WebException wex)

            {

                // Display the exception details

                Console.WriteLine(wex.ToString());

            }

        }

    }

}

 

Build and run the project (Start Without Debugging). The following are sample outputs.

 

C# Request and Response Web Client Program Example - a sample output

 

C# Request and Response Web Client Program Example - a sample output, reading a local html file

 

VB .NET Request and Response Web Client Program Example

 

Create a new console application project. You can use the project and solution name as shown in the following Figure.

 

VB .NET Request and Response Web Client Program Example - a new console application project creation in Visual Studio 2008

 

Add the following code.

 

Imports System

Imports System.IO

Imports System.Net

Imports System.Text

 

' <summary>

' Demonstrates the use of WebClient by

' downloading the contents of a URL and displaying them on the console.

' </summary>

Module Module1

 

    Sub Main()

        ResolveResource("http://www.wordpress.org/index.php")

        Console.WriteLine()

        Console.WriteLine("Reading local HTML file...")

        ' Make sure the file is there for testing, change accordingly...

        ResolveResource("file://c:\\temp\\test.htm")

    End Sub

 

    Sub ResolveResource(ByVal address As String)

        Try

            ' Download the content

            Dim client As New WebClient

            Dim contentBytes() As Byte = client.DownloadData(address)

            Console.WriteLine("DownloadData() is OK...")

 

            ' Convert the content bytes to a string

            Dim content As String = Encoding.ASCII.GetString(contentBytes)

            Console.WriteLine("GetString() is OK...")

 

            ' Display the content to the console

            Console.WriteLine(content)

            Console.WriteLine("WriteLine() is OK...")

 

        Catch wex As WebException

            ' Display the exception details

            Console.WriteLine(wex.ToString())

        End Try

    End Sub

End Module

 

Build and run the project (Start Without Debugging). The following are sample outputs.

 

VB .NET Request and Response Web Client Program Example - a sample output

 

VB .NET Request and Response Web Client Program Example - a sample output, reading a local file

 

 

 


 

< Request-Response Model & C++ .NET Examples | Main | Chap 7: DNS & IP Addressing >