Execute.HttpRequest

.NET Framework class library used the send HTTP requests and parse the response.

For HTML DOM parsing and manipulation, this library uses AngleSharp.

Quick Start

Make Execute.HttpRequest available in your current Windows PowerShell session using the script below.

  • Set-ExecutionPolicy Unrestricted -Scope Process -Force;
  • [System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 3072;
  • iex (irm (irm "https://api.github.com/repos/nstevens1040/Execute.HttpRequest/releases/latest").assets[0].browser_download_url)
Copy

Test it.

  • $r = [Execute.HttpRequest]::Send("https://nstevens1040.github.io/Execute.HttpRequest/")
  • $r.ResponseText
Copy

This should output the response text from nstevens1040.github.io/Execute.HttpRequest.

In C# / .NET Framework

Clone the repository and build it in Visual Studio. It will create the library file, Execute.HttpRequest.dll in .\Execute.HttpRequest\bin\Debug\Execute.HttpRequest.dll.
Make a reference to Execute.HttpRequest.dll and use the Send method:

  • namespace JustAnExample
  • {
  •     using Execute;
  •     using System;
  •     using System.Net;
  •     using System.Collections.Specialized;
  •     using System.Net.Http;
  •     public class Test
  •     {
  •         public static RetObject Request()
  •         {
  •             OrderedDictionary headers = new OrderedDictionary();
  •             headers.Add("x-test-header", Guid.NewGuid().ToString());
  •             CookieCollection cookies = new CookieCollection()
  •             {
  •                 new Cookie()
  •                 {
  •                     Name = "test_cookie",
  •                     Value = Guid.NewGuid().ToString(),
  •                     Path = "/post",
  •                     Domain = "postman-echo.com",
  •                     Secure = true,
  •                     Expires = DateTime.Parse(@"1970-01-01").AddSeconds(1841294985),
  •                     HttpOnly = false
  •                 }
  •             };
  •             return (Execute.HttpRequest.Send(
  •                 "https://postman-echo.com/post",
  •                 HttpMethod.Post,
  •                 headers,
  •                 cookies,
  •                 "application/x-www-form-urlencoded",
  •                 "data=that&i%27m=sending&via=httpost"
  •             ));
  •         }
  •     }
  • }
Copy

Add-Type in Windows PowerShell 5.1

Clone the repository and build it. It will create the library file, Execute.HttpRequest.dll in .\Execute.HttpRequest\bin\Debug\Execute.HttpRequest.dll.
You can use the Path argument to the Add-Type command to specify the file path to the DLL.

  • Add-Type -Path ".\source\repos\Execute.HttpRequest\Execute.HttpRequest\bin\Debug\Execute.HttpRequest.dll"
  • Copy

Either way, once the command completes, you can use the library in PowerShell like this:

  • $headers = [ordered]@{"x-test-header" = [Guid]::NewGuid();}
  • $cookies = [System.Net.CookieCollection]::New()
  • $cookies.Add(
  •     [System.Net.Cookie]@{
  •         Name = "test_cookie";
  •         Value = [Guid]::NewGuid();
  •         Path = "/post";
  •         Domain = "postman-echo.com";
  •         Secure = $true;
  •         Expires = [DateTime]::Parse("1970-01-01").AddSeconds(1841294985);
  •         HttpOnly = $false;
  •     }
  • )
  • $response = [Execute.HttpRequest]::Send(
  •     "https://postman-echo.com/post",
  •     [System.Net.Http.HttpMethod]::Post,
  •     $headers,
  •     $cookies,
  •     "application/x-www-form-urlencoded",
  •     "data=that&i%27m=sending&via=httpost"
  • )
Copy

Positional parameters

The Send method takes 1 to 7 parameters:

Position Type Name Required? expand all
0 System.String uri Yes more
description:

The full URI to the resource you are requesting.

example:

  • string uri = "https://postman-echo.com/post";

notes:

This is the only required parameter.


1 System.Net.Http.HttpMethod method No more
description:

The HTTP method you're using to send the request.

example:

  • HttpMethod method = System.Net.Http.HttpMethod.Post;

notes:

Must be one of GET, POST, PUT, DELETE, TRACE, OPTIONS, or HEAD. Defaults to GET if not specified.


2 System.Collections.Specialized.OrderedDictionary headers No more
description:

Http headers (not Content-* ) to send along with the HTTP request.

example:

  • OrderedDictionary headers = new OrderedDictionary();
  • headers.Add("x-test-header", Guid.NewGuid().ToString());

notes:
  • Content-Length is handled automatically.
  • Content-Type can be specified using the fifth parameter, if necessary
  • User-Agent, if not specified, defaults to Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36
  • Path, if not specified, is handled using new Uri(uri).PathAndQuery


3 System.Net.CookieCollection cookies No more
description:

CookieCollection object populated with 1 or more System.Net.Cookie objects to send along with the HTTP request.

example:

  • CookieCollection cookies = new CookieCollection()
  • {
  •     new Cookie()
  •     {
  •         Name = "test_cookie",
  •         Value = Guid.NewGuid().ToString(),
  •         Path = "/post",
  •         Domain = "postman-echo.com",
  •         Secure = true,
  •         Expires = DateTime.Parse(@"1970-01-01").AddSeconds(1841294985),
  •         HttpOnly = false
  •     }
  • };


4 System.String contentType No more
description:

Mimetype string to include if you're sending data along with your HTTP request.

example:

  • string contentType = "application/x-www-form-urlencoded";


5 System.String body No more
description:

Data that you're sending along with your HTTP request.

example:

  • string body = "data=that&i%27m=sending&via=httpost";

notes:

HttpMethod must be either POST or PUT.


6 System.String filepath No more
description:

Specify a path to a file to send in an HTTP request as multipart/form-data content.

example:

  • string filepath = @"C:\this\is\the\path\to\the\file.txt";


Return object

The Send method returns and instance of an object with the typename Execute.RetObject.
The object contains 5 properties:

Name Type Description
HttpResponseHeaders System.Collections.Specialized.OrderedDictionary Cookies returned from HTTP request
HtmlDocument AngleSharp.Dom.IDocument HTML document DOM parsed with AngleSharp
HttpResponseHeaders System.Collections.Specialized.OrderedDictionary Headers returned from HTTP request
HttpResponseMessage System.Net.Http.HttpResponseMessage Initial object returned from HttpClient.SendAsync()
ResponseText System.String Text body of the HTTP response