window.ENTITIES={'/api/snippets/cs/c%23%20http%20request%20example%20with%20client%20socket':[{"result":true,"message":null,"batch":{"type":"cs","name":"c# http request example with client socket","items":[{"id":"1b34kD","type":"cs","name":"C# HTTP request example with client socket","content":"using System;\nusing System.Net;\nusing System.Net.Sockets;\nusing System.Text;\n\npublic class Program\n{\n public static void Main()\n {\n string host = \"dirask.com\";\n int port = 80;\n\n IPHostEntry entry = Dns.GetHostEntry(host); // getting avaialble IP addresses from DNS using host name\n\n Socket socket = new Socket(SocketType.Stream, ProtocolType.Tcp);\n\n try\n {\n // connecting:\n\n socket.Connect(entry.AddressList, port);\n\n // request:\n\n string requestText = \"GET / HTTP/1.1\\n\\r\" +\n \"Host: \" + host + \"\\n\\r\" +\n \"\";\n byte[] requestBytes = Encoding.UTF8.GetBytes(requestText);\n\n socket.Send(requestBytes);\n\n // response:\n\n byte[] responseBuffer = new byte[1024];\n int responseSize = socket.Receive(responseBuffer);\n\n string responseText = Encoding.UTF8.GetString(responseBuffer, 0, responseSize);\n\n Console.WriteLine(responseText); // HTTP/1.1 400 Bad Request\n // Server: cloudflare\n // Date: Thu, 24 Mar 2022 20:40:49 GMT\n // Content-Type: text/html\n // Content-Length: 155\n // Connection: close\n // CF-RAY: -\n // \n // \n //
400 Bad Request\n // \n // 400 Bad Request
\n //
cloudflare\n // \n // \n }\n catch (Exception ex)\n {\n Console.Error.WriteLine(ex.ToString());\n }\n finally\n {\n // cleaning:\n \n socket.Dispose();\n\n // or: socket.Shutdown(SocketShutdown.Both); with socket.Close();\n }\n }\n}","source":"","author":{"id":"VDr5Q0","name":"DEX7RA","avatar":"1629030396903__VDr5Q0__w40px_h40px.png","points":580,"role":"BASIC"},"creationTime":1648155321000,"updateTime":1648158519000,"removalTime":null},{"id":"D9WVdj","type":"cs","name":"c# http request example with client socket","content":"using System;\nusing System.Net;\nusing System.Net.Sockets;\nusing System.Text;\n\npublic class Program\n{\n public static void Main()\n {\n string hostname = \"dirask.com\";\n int port = 80;\n\n using (TcpClient socket = new TcpClient(hostname, port))\n using (NetworkStream stream = socket.GetStream())\n using (TextWriter writer = new StreamWriter(stream, Encoding.UTF8))\n using (TextReader reader = new StreamReader(stream, Encoding.UTF8))\n {\n // request:\n\n string requestText = \"GET / HTTP/1.1\\n\\r\" +\n \"Host: \" + hostname + \"\\n\\r\" +\n \"\";\n\n writer.Write(requestText);\n writer.Flush();\n\n // response:\n\n string responseText = \"\";\n\n while (true)\n {\n String? line = reader.ReadLine();\n\n if (line == null)\n break;\n\n responseText += line + \"\\n\";\n }\n\n Console.WriteLine(responseText); // HTTP/1.1 400 Bad Request\n // Server: cloudflare\n // Date: Thu, 24 Mar 2022 20:40:49 GMT\n // Content-Type: text/html\n // Content-Length: 155\n // Connection: close\n // CF-RAY: -\n // \n // \n // 400 Bad Request\n // \n // 400 Bad Request
\n //
cloudflare\n // \n // \n }\n }\n}","source":"","author":{"id":"V0JqLo","name":"Inayah-Alexander","avatar":"1629131792850__V0JqLo__w40px_h40px.jpg","points":767,"role":"BASIC"},"creationTime":1649003946000,"updateTime":null,"removalTime":null}]}}]};