C#Webserver只向客户端发送HTML代码

时间:2017-02-25 11:37:38

标签: c# http server

public void Initialise()
    {
        listener = new StreamSocketListener();

        // listen on port 80, this is the standard HTTP port (use a different port if you have a service already running on 80)
        listener.BindServiceNameAsync("80").AsTask();
        listener.ConnectionReceived += async (sender, args) =>
        {
        // call the handle request function when a request comes in
        HandleRequest(sender, args);
        };


    public async void HandleRequest(StreamSocketListener sender, StreamSocketListenerConnectionReceivedEventArgs args)
    {
        StringBuilder request = new StringBuilder();

        // Handle a incoming request
        // First read the request
        using (IInputStream input = args.Socket.InputStream)
        {
            byte[] data = new byte[BufferSize];
            IBuffer buffer = data.AsBuffer();
            uint dataRead = BufferSize;
            while (dataRead == BufferSize)
            {
                await input.ReadAsync(buffer, BufferSize, InputStreamOptions.Partial);
                request.Append(Encoding.UTF8.GetString(data, 0, data.Length));
                dataRead = buffer.Length;
            }
        }

        // Send a response back
        using (IOutputStream output = args.Socket.OutputStream)
        {
            using (Stream response = output.AsStreamForWrite())
            {
                // For now we are just going to reply to anything with Hello World!

                //byte[] bodyArray = Encoding.UTF8.GetBytes("<html><body>Hello, World!</body></html>");
                string page = "";
                var folder = Windows.ApplicationModel.Package.Current.InstalledLocation;
                var file = await folder.GetFileAsync("index.html");
                var readFile = await Windows.Storage.FileIO.ReadLinesAsync(file);
                foreach(var line in readFile)
                {
                    page += line;
                }
                byte[] bodyArray = Encoding.UTF8.GetBytes(page);

                var bodyStream = new MemoryStream(bodyArray);
                // This is a standard HTTP header so the client browser knows the bytes returned are a valid http response
                var header = "HTTP/1.1 200 OK\r\n" +
                            $"Content-Length: {bodyStream.Length}\r\n" +
                                "Connection: close\r\n\r\n";

                byte[] headerArray = Encoding.UTF8.GetBytes(header);

                // send the header with the body included to the client
                await response.WriteAsync(headerArray, 0, headerArray.Length);
                await bodyStream.CopyToAsync(response);
                await response.FlushAsync();
            }
        }

    }

        private StreamSocketListener listener; // the socket listner to listen for TCP requests
                                               // Note: this has to stay in scope!

        private const uint BufferSize = 8192; // this is the max size of the buffer in bytes 
    }
}

您好,

我在我的c#程序中实现了一个http服务器,用于向客户端发送Web接口。此Web界面(index.html)包含一些css样式和js方法。我的问题是客户端浏览器只显示html代码而没有例如样式。如何发送&#34;整体&#34;包含所有Web界面文件的文件夹到客户端?

0 个答案:

没有答案