使用的HttpWebRequest发送HTML到浏览器(Using HttpWebRequest t

2019-11-02 18:16发布

我怎么能发送消息回浏览器或本地主机,例如,如果我想显示的消息叫....这是在浏览器中测试。

using System;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;

class MyTcpListener
{
    public static void Main()
    {


     try
     {

        // Set the TcpListener on port 13000.
            Int32 port = 80;
            IPAddress localAddr = IPAddress.Parse("127.0.0.1");

            // TcpListener server = new TcpListener(port);
            TcpListener server = new TcpListener(localAddr, port);

            // Start listening for client requests.
            server.Start();

            // Buffer for reading data
            Byte[] bytes = new Byte[256];
            String data = null;

            // Enter the listening loop.
            while (true)
            {
                Console.Write("Waiting for a connection... ");

                // Perform a blocking call to accept requests.
                // You could also user server.AcceptSocket() here.
                TcpClient client = server.AcceptTcpClient();
                Console.WriteLine("Connected!");

                data = null;

                // Get a stream object for reading and writing
                NetworkStream stream = client.GetStream();

                int i;

                // Loop to receive all the data sent by the client.
                while ((i = stream.Read(bytes, 0, bytes.Length)) != 0)
                {
                    // Translate data bytes to a ASCII string.
                    data = System.Text.Encoding.ASCII.GetString(bytes, 0, i);
                    Console.WriteLine(String.Format("Received: {0}", data));

                    // Process the data sent by the client.
                    data = data.ToUpper();

                    byte[] msg = System.Text.Encoding.ASCII.GetBytes(data);

                    // Send back a response.
                    stream.Write(msg, 0, msg.Length);
                    Console.WriteLine("Sending message..");

                 }

                // Shutdown and end connection
                client.Close();
            }
        }
        catch (SocketException e)
        {
            Console.WriteLine("SocketException: {0}", e);
        }

        Console.WriteLine("\nHit enter to continue...");
        Console.Read();
    }
}

Answer 1:

而不是读,直到您检索0字节,你应该阅读,直到你找到一个空行。 在开放的网络连接只有几个街区,直到浏览器的最后一位调用read()发送更多的数据(这也不会因为它发出的一切,正在等着你)。

class MyTcpListener
{
    public static void Main()
    {


        try
        {

            // Set the TcpListener on port 13000.
            Int32 port = 80;
            IPAddress localAddr = IPAddress.Parse("127.0.0.1");

            // TcpListener server = new TcpListener(port);
            TcpListener server = new TcpListener(localAddr, port);

            // Start listening for client requests.
            server.Start();

            // Enter the listening loop.
            while (true)
            {
                Console.Write("Waiting for a connection... ");

                // Perform a blocking call to accept requests.
                // You could also user server.AcceptSocket() here.
                TcpClient client = server.AcceptTcpClient();
                Console.WriteLine("Connected!");


                // Get a stream object for reading and writing
                using(NetworkStream stream = client.GetStream())
                using (StreamReader sr = new StreamReader(stream))
                {
                    List<byte> msg = new List<byte>();
                    // Loop to receive all the data sent by the client.
                    string data;
                    while ((data = sr.ReadLine()) != "")
                    {
                        Console.WriteLine(String.Format("Received: {0}", data));

                        // Process the data sent by the client.
                        data = data.ToUpper();

                        msg.AddRange(System.Text.Encoding.ASCII.GetBytes(data));

                    }

                    // Send back a response.
                    stream.Write(msg.ToArray(), 0, msg.Count);
                    Console.WriteLine("Sending message..");
                }
                client.Close();
            }
        }
        catch (SocketException e)
        {
            Console.WriteLine("SocketException: {0}", e);
        }

        Console.WriteLine("\nHit enter to continue...");
        Console.Read();
    }
}


Answer 2:

using(StreamWriter sw = new StreamWriter(stream))
{
    sw.Write("<html><body>Hello There!</body></html>");
}

这应该写HTML到输出流。 请务必在此之后关闭流。 而且还关闭的TcpClient。



Answer 3:

这似乎是一个控制台应用程序,而不是一个网站,所以在生成的输出Console.WriteLine语句将去调试控制台如果您在Visual Studio中运行它,还是应该在屏幕上显示,如果你运行它作为一个编译应用从命令提示。

什么样的项目,这是一部分?



文章来源: Using HttpWebRequest to send HTML to a Browser