Send HTTP Get Request With Network Credential C#

When sending Http get request to a website, sometimes users need to submit network credentials. For how to send a Http get request, please review related articles in the site. This example below explains how to send a Http Get request with network credentials. 


We can use NetworkCredential class in the System.Net namespace to send username, user password and domain information in the Http Get request. In your C# project, add a class named "HttpRequestUtilityAuthentication" and write the following code. 


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

namespace Com.Example.Csharp
    class HttpRequestUtilityAuthentication
        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 Credential
            String UserId = "someUserId";
            String UserPassword = "somePassword";
            String DomainName = "SomeDomain";
            NetworkCredential networkCredential=new NetworkCredential(UserId,UserPassword,DomainName);
            httpWebRequest.Credentials = networkCredential;
            //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: ");
            //Close Stream Response
            if (responseStream != null) responseStream.Close();

You can call the method above and send Http get request to websites that require network credentials.