Skip to content

TiberiumFusion/CurlThin

 
 

Repository files navigation

Simple fork of CurlThin.

Changes

  • NuGet package ID root changed (from CurlThin to CurlThin-tfusion).
  • Reverted highly incompatible target framework (from .NET Standard 2.1 to .Net Standard 2.0)
  • HyperPipe excised from CurlThin into a separate package CurlThin.HyperPipe (to relegate obnoxious dependencies if unused)
  • Fixed x86 build of CurlThin not working at all (when using official libcurl win binary or other non-MSVC builds)
  • Updated native libcurl resources (from 7.69.1 ca 2020-03 to 8.7.1 ca 2024-03).
  • Updated some minor NuGet dependencies for HyperPipe.

Modified original readme below.

CurlThin

CurlThin is a NET Standard compatible binding library against libcurl. It includes a modern wrapper for curl_multi interface which uses polling with libuv library instead of using inefficient select.

CurlThin has a very thin abstraction layer, which means that writing the code is as close as possible to writing purely in libcurl. libcurl has extensive documentation and relatively strong support of community and not having additional abstraction layer makes it easier to search solutions for your problems.

Using this library is very much like working with cURL's raw C API.

License

Library is MIT licensed.

Installation

Release nupkgs are available from the Releases page. Add them to your project via a local NuGet package repository. These nupkgs are not on the global nuget.org repository.

Package Description
CurlThin-tfusion The C# wrapper for libcurl.
CurlThin-tfusion.HyperPipe Optional convenience interface for using curl_multi.
CurlThin-tfusion.Native Contains the embedded libcurl native binaries for x86 and x64 Windows.

CurlThin-tfusion.Native provides the native curl library for machines that do not have libcurl in their PATH.
If libcurl is already installed and in your PATH, you can skip CurlThin-tfusion.Native.

After you add CurlThin-tfusion.Native to your project, your program must call the following method just once, before you can use cURL:

CurlResources.Init();

It will extract following files to your application output directory

File Description
libcurl.dll The multiprotocol file transfer library, with all dependencies included.
curl-ca-bundle.crt Certificate Authority (CA) bundle. You can use it via CURLOPT_CAINFO.

Examples

Easy interface

GET request

// curl_global_init() with default flags.
var global = CurlNative.Init();

// curl_easy_init() to create easy handle.
var easy = CurlNative.Easy.Init();
try
{
    CurlNative.Easy.SetOpt(easy, CURLoption.URL, "http://httpbin.org/ip");

    var stream = new MemoryStream();
    CurlNative.Easy.SetOpt(easy, CURLoption.WRITEFUNCTION, (data, size, nmemb, user) =>
    {
        var length = (int) size * (int) nmemb;
        var buffer = new byte[length];
        Marshal.Copy(data, buffer, 0, length);
        stream.Write(buffer, 0, length);
        return (UIntPtr) length;
    });

    var result = CurlNative.Easy.Perform(easy);

    Console.WriteLine($"Result code: {result}.");
    Console.WriteLine();
    Console.WriteLine("Response body:");
    Console.WriteLine(Encoding.UTF8.GetString(stream.ToArray()));
}
finally
{
    easy.Dispose();

    if (global == CURLcode.OK)
    {
        CurlNative.Cleanup();
    }
}

POST request

// curl_global_init() with default flags.
var global = CurlNative.Init();

// curl_easy_init() to create easy handle.
var easy = CurlNative.Easy.Init();
try
{
    var postData = "fieldname1=fieldvalue1&fieldname2=fieldvalue2";

    CurlNative.Easy.SetOpt(easy, CURLoption.URL, "http://httpbin.org/post");

    // This one has to be called before setting COPYPOSTFIELDS.
    CurlNative.Easy.SetOpt(easy, CURLoption.POSTFIELDSIZE, Encoding.ASCII.GetByteCount(postData));
    CurlNative.Easy.SetOpt(easy, CURLoption.COPYPOSTFIELDS, postData);
    
    var stream = new MemoryStream();
    CurlNative.Easy.SetOpt(easy, CURLoption.WRITEFUNCTION, (data, size, nmemb, user) =>
    {
        var length = (int) size * (int) nmemb;
        var buffer = new byte[length];
        Marshal.Copy(data, buffer, 0, length);
        stream.Write(buffer, 0, length);
        return (UIntPtr) length;
    });

    var result = CurlNative.Easy.Perform(easy);

    Console.WriteLine($"Result code: {result}.");
    Console.WriteLine();
    Console.WriteLine("Response body:");
    Console.WriteLine(Encoding.UTF8.GetString(stream.ToArray()));
}
finally
{
    easy.Dispose();

    if (global == CURLcode.OK)
    {
        CurlNative.Cleanup();
    }
}

Multi interface

Web scrape StackOverflow questions

See Multi/HyperSample.cs.

About

Lightweight cURL wrapper for C# with support for curl_multi polling interface through libuv

Resources

License

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages

  • C# 97.6%
  • PowerShell 2.4%