Skip to content

Instantly share code, notes, and snippets.

Last active January 28, 2022 17:31
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
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);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment