Created
January 28, 2022 17:29
-
-
Save fzankl/3e914497ee4beab4bb8d7815ce960fd9 to your computer and use it in GitHub Desktop.
Request API over unix socket using Socket 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 Hostname = "localhost"; | |
const string UnixSocketPath = "/tmp/foo.sock"; | |
using var socket = new Socket(AddressFamily.Unix, SocketType.Stream, ProtocolType.IP); | |
var endpoint = new UnixDomainSocketEndPoint(UnixSocketPath); | |
socket.Connect(endpoint); | |
var requestBytes = System.Text.Encoding.UTF8.GetBytes($"GET / HTTP/1.0\r\nHost: {Hostname}\r\nAccept: */*\r\n\r\n"); | |
socket.Send(requestBytes); | |
byte[] receivedBytes = new byte[1024]; | |
socket.Receive(receivedBytes, 1024, SocketFlags.None); | |
Console.WriteLine(System.Text.Encoding.UTF8.GetString(receivedBytes)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment