- using System;
- using System.Net;
- using System.Net.Sockets;
- using System.IO;
- class Program
- {
- static void Main(string[] args)
- {
- string host = "";
- string file = "";
- try
- {
- TcpClient tcpClient = new TcpClient(host, 80);
- NetworkStream nStream = tcpClient.GetStream();
- StreamReader sReader = new StreamReader(nStream);
- StreamWriter sWriter = new StreamWriter(nStream);
- sWriter.WriteLine("GET /" + file + " HTTP/1.1");
- sWriter.WriteLine("Host: " + host);
- sWriter.WriteLine("");
- sWriter.Flush();
- String response = "";
- while ((response = sReader.ReadLine()) != null)
- {
- Console.WriteLine(sReader.ReadLine());
- }
- }
- catch (Exception e)
- {
- Console.WriteLine(e.Message);
- }
- Console.ReadKey();
- }
- }
Connect To Web Server Using TcpClient In C#
This snippet connects to a web server and requests for a page. It uses the TcpClient to send header information to the server.
No comments:
Post a Comment