使用HttpWebRequest将HTML发送到浏览器 [英] Using HttpWebRequest to send HTML to a Browser

查看:108
本文介绍了使用HttpWebRequest将HTML发送到浏览器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,如果我想显示名为...的消息,我该如何将消息发送回浏览器或本地主机.

how can i send message back to the browser or localhost for example if i want to display message called.... This is test in browser.

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();
    }
}

推荐答案

除了读取直到读取0个字节外,您应该读取直到找到空白行,而不是读取.在开放式网络连接的最后一位调用Read()只是阻塞,直到浏览器发送更多数据为止(不会发送,因为它已发送所有内容并在等待您).

Instead of reading until you retrieve 0 bytes, you should read until you find a blank line. Calling Read() on the last bit of the open network connection just blocks until the browser sends more data (which it won't because it's sent everything and is waiting on you).

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();
    }
}

这篇关于使用HttpWebRequest将HTML发送到浏览器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆