[Edit]
+
0
-
0
c# http request example with client socket
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58using System; using System.Net; using System.Net.Sockets; using System.Text; public class Program { public static void Main() { string hostname = "dirask.com"; int port = 80; using (TcpClient socket = new TcpClient(hostname, port)) using (NetworkStream stream = socket.GetStream()) using (TextWriter writer = new StreamWriter(stream, Encoding.UTF8)) using (TextReader reader = new StreamReader(stream, Encoding.UTF8)) { // request: string requestText = "GET / HTTP/1.1\n\r" + "Host: " + hostname + "\n\r" + ""; writer.Write(requestText); writer.Flush(); // response: string responseText = ""; while (true) { String? line = reader.ReadLine(); if (line == null) break; responseText += line + "\n"; } Console.WriteLine(responseText); // HTTP/1.1 400 Bad Request // Server: cloudflare // Date: Thu, 24 Mar 2022 20:40:49 GMT // Content-Type: text/html // Content-Length: 155 // Connection: close // CF-RAY: - // // <html> // <head><title>400 Bad Request</title></head> // <body> // <center><h1>400 Bad Request</h1></center> // <hr><center>cloudflare</center> // </body> // </html> } } }