How to implement Http Get Request

This code snip will explain how to send GET request to a web server via TcpClient.

public void GetRequest(String url, String Header, bool verbose, String filename)

        {

           

            if (!url.Contains("://"))

                url = "http://" + url;

            String Domain = new Uri(url).Host;

            String pq = new Uri(url).PathAndQuery;

            TcpClient sock = new TcpClient(Domain, 80);

            Stream stream = sock.GetStream();

            Byte[] req = Encoding.ASCII.GetBytes("GET " + pq + " HTTP/1.0\n\n" + Header + "\n\n");

            stream.Write(req, 0, req.Length);

            stream.Flush();

            StreamReader inp = new StreamReader(stream);

            String line;

            bool docstart = false;

            while ((line = inp.ReadLine()) != null)

            {

                if (!docstart)

                {

                    if (line == "")

                        docstart = true;

                }

                if (!verbose && !docstart)

                    continue;

                if (filename != "")

                {

                    File.AppendAllText(filename, line + Environment.NewLine);

 

                }

                else

                    Console.WriteLine(line);

            }

            inp.Close();

            return;

        }