简体   繁体   中英

HTTP Request to get HTML code in C#

I want to get the HTML code from http://www.w3schools.com/ Here is my code:

    static void Main(string[] args)
    {
        TcpClient client = new TcpClient("www.w3schools.com", 80);
        client.SendTimeout = 3000;
        client.ReceiveTimeout = 3000;
        StreamWriter writer = new StreamWriter(client.GetStream());
        StreamReader reader = new StreamReader(client.GetStream());
        writer.WriteLine("GET www.w3schools.com HTTP/1.1");
        writer.WriteLine("Host: www.w3schools.com");
        writer.WriteLine();
        writer.Flush();

        string response = reader.ReadToEnd();

        Console.WriteLine("Got Response: {0}", response);

        Console.ReadLine();
    }

But I get the following: 问题 Where I'm wrong?

The second element of the GET line should be the query path, not the domain name. This should work:

writer.WriteLine("GET / HTTP/1.1");
writer.WriteLine("Host: www.w3schools.com");
  1. For this TcpClient to work you need to have a Wporking TcpServer.
  2. The correct uri should be like (" https://www.w3schools.com/html/default.asp ") where after .com the file name will b provided.

The below code will work even without TcpServer.

public static void getSavedHtmlCode()
    {
        string html = string.Empty;


        try
        {
            var request = System.Net.HttpWebRequest.Create(string.Format("{0}", "https://www.w3schools.com/html/default.asp"));
            request.Method = "GET";
            var response = (HttpWebResponse)request.GetResponse();

            //prepare as html
            //html = new StreamReader(response.GetResponseStream()).ReadToEnd();

            // Get the stream associated with the response.
            Stream receiveStream = response.GetResponseStream();

            // Pipes the stream to a higher level stream reader with the required encoding format. 
            StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);

            //prepare as html
            html = readStream.ReadToEnd();

            Console.WriteLine("Response stream received.");
            Console.WriteLine(html);

        }
        catch (Exception ex)
        {
            Console.WriteLine(ex);
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM