https://github.com/nstevens1040/execute.httprequest
.NET Framework class library used the send HTTP requests and parse the response.
https://github.com/nstevens1040/execute.httprequest
class-library cookies csharp dotnet-framework http http-client http-delete http-get http-post http-request http-response http-trace https windows-powershell
Last synced: 3 months ago
JSON representation
.NET Framework class library used the send HTTP requests and parse the response.
- Host: GitHub
- URL: https://github.com/nstevens1040/execute.httprequest
- Owner: nstevens1040
- License: mit
- Created: 2021-01-13T02:17:45.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2024-10-22T15:14:27.000Z (7 months ago)
- Last Synced: 2025-02-01T20:11:24.779Z (4 months ago)
- Topics: class-library, cookies, csharp, dotnet-framework, http, http-client, http-delete, http-get, http-post, http-request, http-response, http-trace, https, windows-powershell
- Language: HTML
- Homepage: https://nstevens1040.github.io/Execute.HttpRequest/
- Size: 22.4 MB
- Stars: 4
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
[](https://ci.appveyor.com/project/nstevens1040/execute-httprequest-scamo)
[](https://codecov.io/gh/nstevens1040/Execute.HttpRequest)
[](https://github.com/nstevens1040/Execute.HttpRequest/blob/master/LICENSE)
[](https://ko-fi.com/M4M23DLL6)
[](https://paypal.me/nstevens312)
[](https://nstevens1040.github.io/donthatedonate/)
[](https://nstevens1040.github.io/sparesomechange/)
# Execute.HttpRequest
[View the html version of this README](https://nstevens1040.github.io/Execute.HttpRequest)
.NET Framework class library used the send HTTP requests and parse the response.
This library uses [my fork](https://github.com/nstevens1040/AngleSharp.DOMParser) of [AngleSharp](https://github.com/AngleSharp/AngleSharp) to parse HTML strings into a document object model.
# Installation
## Quick Start
Make Execute.HttpRequest available in your current Windows PowerShell session using the script below.
```ps1
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.SecurityProtocolType]::Tls12
iex (irm (irm "https://api.github.com/repos/nstevens1040/Execute.HttpRequest/releases/latest").assets[0].browser_download_url)
```
![]()
Test it.
```ps1
$r = [Execute.HttpRequest]::Send("https://nstevens1040.github.io/Execute.HttpRequest/")
$r.ResponseText
```
This should output the response text from nstevens1040.github.io/Execute.HttpRequest.
![]()
## .NET Framework project in Visual Studio
Clone the repository and build it in Visual Studio. It will create the library file, **Execute.HttpRequest.dll** in `.\source\repos\Execute.HttpRequest\Execute.HttpRequest\bin\Debug\Execute.HttpRequest.dll`.
Make a reference to **Execute.HttpRequest.dll** and use the **Send** method:
```cs
using Execute;
using System;
using System.Net;
using System.Collections.Specialized;
using System.Net.Http;
namespace JustAnExample
{
public class Test
{
public RetObject Request(CookieCollection cookies, OrderedDictionary headers)
{
RetObject response = HttpRequest.Send(
"https://fakedomain.com/post",
HttpMethod.Post,
headers,
cookies,
"application/x-www-form-urlencoded",
"data=that&i%27m=sending&via=httpost"
);
return response;
}
}
}
```
## Windows PowerShell 5.1
### Add-Type using the path to the DLL
Clone the repository and build it in Visual Studio. It will create the library file, **Execute.HttpRequest.dll** in `.\source\repos\Execute.HttpRequest\Execute.HttpRequest\bin\Debug\Execute.HttpRequest.dll`.
Now you can use the **Path** parameter, specifying the path to the DLL file as the parameter to Add-Type instead of TypeDefinition.
```ps1
Add-Type -Path .\source\repos\Execute.HttpRequest\Execute.HttpRequest\bin\Debug\Execute.HttpRequest.dll
```
Once the command completes, you can use the library in PowerShell like this:
```ps1
$re = [Execute.HttpRequest]::Send(
"https://fakedomain.com/post",
[System.Net.Http.HttpMethod]::Post,
([ordered]@{"x-csrf-token"="blahblahblahfaketokenblahblah"}),
$cookies, # CookieContainer example below
"application/x-www-form-urlencoded",
"data=that&i%27m=sending&via=httpost"
)
```
# Usage
## Positional parameters:
### Uri
| | |
|----------------|---------------------------------------------------------|
| **Type** | System.String |
| **Position** | 0 |
| **Purpose** | The full URI to the resource you are requesting. |
| **Required?** | Yes |
```cs
string uri = @"https://twitter.com/sessions";
```
### Method
| | |
|----------------|-----------------------------------------------------|
| **Type** | System.Net.Http.HttpMethod |
| **Position** | 1 |
| **Purpose** | The HTTP method you're using to send the request. Must be one of **GET, POST, PUT, DELETE, TRACE, OPTIONS, or HEAD**. |
| **Required?** | No (**Defaults to GET** if not specified) |
```cs
HttpMethod method = HttpMethod.Post;
```
### Headers
| | |
|----------------|-----------------------------------------------------|
| **Type** | System.Collections.Specialized.OrderedDictionary |
| **Position** | 2 |
| **Purpose** | Http headers (**not Content-\***) to send along with the HTTP request. |
| **Required?** | No (**HTTP headers Path, User-Agent, and Content-Length are sent automatically**) |
```cs
OrderedDictionary headers = new OrderedDictionary();
headers.Add("x-csrf-token","blahblahblahfaketokenblahblah");
```
### Cookies
| | |
|----------------|-----------------------------------------------------|
| **Type** | System.Net.CookieCollection |
| **Position** | 3 |
| **Purpose** | CookieCollection object populated with 1 or more System.Net.Cookie objects to send along with the HTTP request. |
| **Required?** | No |
```cs
CookieCollection cookies = new CookieCollection();
cookies.Add((new Cookie(){
Name="MyCookieName",
Value="MyCookieValue",
Path="/",
Domain=".fakedomain.com",
Expires=DateTime.Now.AddDays(365)
}));
```
### Content-Type
| | |
|----------------|-------------------------------------------------|
| **Type** | System.String |
| **Position** | 4 |
| **Purpose** | Mimetype string to include if you're sending data along with your HTTP request. |
| **Required?** | No |
```cs
string contentType = @"application/x-www-form-urlencoded";
```
### Body
| | |
|----------------|-------------------------------------------------|
| **Type** | System.String |
| **Position** | 5 |
| **Purpose** | Data that you're sending along with your HTTP request. **HTTP method must be either POST or PUT.** |
| **Required?** | No |
```cs
string body = @"Any=string&will=do%2c&so=long&as=it&serves=your&purpose=well";
```
### FilePath
| | |
|----------------|-------------------------------------------------|
| **Type** | System.String |
| **Position** | 6 |
| **Purpose** | If you're sending a file along with a multipart/form-data POST request, then specify the path to the file you are sending here. |
| **Required?** | No |
```cs
string filepath = Environment.GetEnvironmentVariable(@"userprofile") + "\\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 |
|--------------------------|----------------------------------------------------|------------------------------------------------------------|
| **CookieCollection** | System.Net.CookieCollection | Cookies returned from HTTP request |
| **HtmlDocument** | System.Object | HTML document parsed via AngleSharp.Html.Dom.HtmlDocument |
| **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 |
The advantage of using this library is the ability to take, for example, the CookieCollection object returned from one request and pipe those cookies into the next request.
To illustrate:
```ps1
$re = [Execute.HttpRequest]::Send(
"https://fakedomain.com/post",
[System.Net.Http.HttpMethod]::Post,
([ordered]@{"x-csrf-token"="blahblahblahfaketokenblahblah"}),
$cookies,
"application/x-www-form-urlencoded",
"data=that&i%27m=sending&via=httpost"
)
$next = [Execute.HttpRequest]::Send(
"https://fakedomain.com/someotherresource",
[System.Net.Http.HttpMethod]::Get,
$re.HttpResponseHeaders,
$re.CookieCollection
)
```