Last active
January 28, 2022 17:31
-
-
Save fzankl/ca79af4fdc7ed1605977f1bb564c608e to your computer and use it in GitHub Desktop.
Request API over unix socket using SocketsHttpHandler class
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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