Selenium WebDriver Java Framework Course Limited Time Offer for $20

Selenium WebDriver Java Framework Course Limited Time Offer for $20

 

Print

Send HTTP Get Request C#

This article explains how to send a HTTP Get request to a webpage or website using HttpWebRequest class in C#. In this example, we retrieve the response of HTTP get request to a website "http://demo.mahara.org". 

 

1. Create a class named HttpRequestUtility in your c# project and write the following code. 

using System;
using System.IO;
using System.Net;

namespace Com.Example.Csharp
{
    class HttpRequestUtility
    {
        public void GetRequest(String uri)
        {
            //Create a HTTP Web Request Instance
            var httpWebRequest = (HttpWebRequest) WebRequest.Create(uri);
            //Set the request type to Get
            httpWebRequest.Method = "GET";
            //Set the maximum redirection
            httpWebRequest.MaximumAutomaticRedirections = 3;
            //Set time out
            httpWebRequest.Timeout = 5000;
            Console.WriteLine("Sending HTTP Request");
            //Create a HTTP Web Response Instance
            var httpWebResponse = (HttpWebResponse) httpWebRequest.GetResponse();
            //Get the Response Stream
            var responseStream = httpWebResponse.GetResponseStream();
            if (responseStream != null)
            {
                //Read Response Stream Using Stream Reader
                var streamReader=new StreamReader(responseStream);
                Console.WriteLine("HTTP Response is: ");
                Console.WriteLine(streamReader.ReadToEnd());
            }
            //Close Stream Response
            if (responseStream != null) responseStream.Close();
        }
    }
}

Write the code below in the Main method of the Program class.

using System;

namespace Com.Example.Csharp
{
    class Program
    {
        private static void Main(string[] args)
        {
            //Get Http Response
            HttpRequestUtility httpRequestUtility=new HttpRequestUtility();
            httpRequestUtility.GetRequest("http://demo.mahara.org");
            Console.ReadKey();
        }
    }
}

Run the Main method in the Program class and see the result on the console . You will see all the html response on the console.