Skip to content

Instantly share code, notes, and snippets.

@fzankl
Last active January 28, 2022 17:31
Show Gist options
  • Save fzankl/ca79af4fdc7ed1605977f1bb564c608e to your computer and use it in GitHub Desktop.
Save fzankl/ca79af4fdc7ed1605977f1bb564c608e to your computer and use it in GitHub Desktop.
Request API over unix socket using SocketsHttpHandler class
using System.Net.Sockets;
const string BaseAddress = "http://localhost";
const string UnixSocketPath = "/tmp/foo.sock";
var httpHandler = new SocketsHttpHandler
{
ConnectCallback = async (context, token) =>
{
var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP);
var endpoint = new UnixDomainSocketEndPoint(UnixSocketPath);
await socket.ConnectAsync(endpoint).ConfigureAwait(false);
return new NetworkStream(socket, ownsSocket: false);
}
};
var client = new HttpClient(httpHandler);
client.BaseAddress = new Uri(BaseAddress);
var response = await client.GetAsync("/").ConfigureAwait(false);
var content = await response.Content.ReadAsStringAsync().ConfigureAwait(false);
Console.WriteLine(content);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment