Coder Social home page Coder Social logo

nstevens1040 / execute.httprequest Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 1.0 10.03 MB

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

Home Page: https://nstevens1040.github.io/Execute.HttpRequest/

License: MIT License

C# 63.88% HTML 36.12%
http-response http-request http https http-client dotnet-framework class-library csharp http-get http-post

execute.httprequest's Introduction

Build status codecov MIT License Ko-Fi PayPal Bitcoin Monero

Execute.HttpRequest

.NET Framework class library used the send HTTP requests and parse the response.
This library uses my fork of 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.

[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.

$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:

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.

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:

$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
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)
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)
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
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
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
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
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:

$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
)

execute.httprequest's People

Contributors

nstevens1040 avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Forkers

coders-vechai

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.