Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

In c# complete the code in the TODO area of the code below. This can be done in

ID: 3918696 • Letter: I

Question

In c# complete the code in the TODO area of the code below. This can be done in any of the following ways. WebRequest and HttpWebResponse classes, a WebClient object, or an HTTPClient object.

In this lab, you will be writing your own C# web client that communicates to a web server and downloads a short message. There are multiple ways to implement the web client, a few of them being to utilize the WebRequest and HttpWebResponse classes, a WebClient object, or an HTTPClient object. Your web client should use a download method to contact the web server and display the server's response to the user. The template code already creates a working web server for you, so you'll only need to handle the client-side code.

Network.cs

using System.IO;

using System.Net;

using System.Text;

using System;

using System.Threading;

namespace Networking

{

class Program

{

static void Main()

{

//Start the server

Server();

Console.WriteLine(" ---------------------------------------------------------------- Press any key to start the client...");

Console.ReadKey(true);

//Start the client

WebClient();

//Wrap up

Console.WriteLine(" -------Press Enter key when done.---------");

Console.ReadKey(true);

}

async static void Server()

{

//Prompt the user for the URI and port

Console.WriteLine(" What is the URI of the server? (http://localhost) ");

string serverURI = Console.ReadLine();

Console.WriteLine(" And what is the port number? (1300) ");

string portNum = Console.ReadLine();

try

{

//Creates the listener; this is the part that "listens" for a client to connect

HttpListener listener = new HttpListener();

listener.Prefixes.Add(serverURI + ":" + portNum + "/");

//Start the listener

Console.WriteLine(" Starting Server...");

listener.Start();

Console.WriteLine("Server Started ");

while (true)

{

//The server code will wait right here for a client to connect; in the meantime, control returns to the main method.

//When a client does connect, this code will resume until it loops back around to "await" again.

HttpListenerContext context = await listener.GetContextAsync();

//Create a response to send back to the client

string msg = " Message from Server: Congrats! You have started a server and accessed that server with your client!";

context.Response.ContentLength64 = Encoding.UTF8.GetByteCount(msg);

context.Response.StatusCode = (int)HttpStatusCode.OK;

//Convert that response into a stream and write it with a StreamWriter

using (Stream stream = context.Response.OutputStream)

{

using (StreamWriter writer = new StreamWriter(stream))

{

writer.Write(msg);

}

}

Console.WriteLine("Message sent to client");

}

}

catch (Exception err)

{

//If any error occurs, write its information to the console

//The server will then shut down

Console.WriteLine(err.ToString());

}

}

static void WebClient()

{

//TODO: Connect to the server you set up previously, send it a message, and output its response to the user.

}

}

}

Explanation / Answer

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;

namespace WebClient
{
class Program
{
static string ServerPath = null;
async static void Server()
{

//Prompt the user for the URI and port

Console.WriteLine(" What is the URI of the server? (http://localhost) ");

string serverURI = Console.ReadLine();

Console.WriteLine(" And what is the port number? (1300) ");

string portNum = Console.ReadLine();

try
{

//Creates the listener; this is the part that "listens" for a client to connect

HttpListener listener = new HttpListener();
Program.ServerPath = serverURI + ":" + portNum + "/";
listener.Prefixes.Add(Program.ServerPath);

//Start the listener

Console.WriteLine(" Starting Server...");

listener.Start();

Console.WriteLine("Server Started ");

while (true)
{

//The server code will wait right here for a client to connect; in the meantime, control returns to the main method.

//When a client does connect, this code will resume until it loops back around to "await" again.

HttpListenerContext context = await listener.GetContextAsync();

//Create a response to send back to the client

string msg = " Message from Server: Congrats! You have started a server and accessed that server with your client!";

context.Response.ContentLength64 = Encoding.UTF8.GetByteCount(msg);

context.Response.StatusCode = (int)HttpStatusCode.OK;

//Convert that response into a stream and write it with a StreamWriter

using (Stream stream = context.Response.OutputStream)
{

using (StreamWriter writer = new StreamWriter(stream))
{

writer.Write(msg);

}

}

Console.WriteLine("Message sent to client");

}

}

catch (Exception err)
{

//If any error occurs, write its information to the console

//The server will then shut down

Console.WriteLine(err.ToString());

}

}
static void WebClient()
{

//TODO: Connect to the server you set up previously, send it a message, and output its response to the user.

using (HttpClient client = new HttpClient()) {
client.BaseAddress =new Uri(Program.ServerPath);
HttpResponseMessage Responce= client.GetAsync("/").Result;
string result= Responce.Content.ReadAsStringAsync().Result;
Console.WriteLine("Client is connected successfully.");
Console.WriteLine("Massege is getting from serveris shown below....");
Console.WriteLine(result);
Console.WriteLine("Client is disconnected successfully.");

}

}
static void Main(string[] args)
{
//Start the server

Server();

Console.WriteLine(" ---------------------------------------------------------------- Press any key to start the client...");

Console.ReadKey(true);

//Start the client

WebClient();

//Wrap up

Console.WriteLine(" -------Press Enter key when done.---------");

Console.ReadKey(true);
}
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote