簡體   English   中英

C#簡單Web服務器圖像讀入

[英]C# Simple Web Server Image Read-In

所以我回來了關於我的Web服務器的另一個問題。 自上一個問題以來,我已經為其添加了一些新功能,但是現在我遇到了另一個問題。

我試圖允許服務器提供通過HTML調用的圖像。 但是,由於某種原因,該圖像未顯示在網頁上。 我已經遍歷了代碼,它似乎確實是通過文件發送的,但是它並沒有顯示在另一端。 這是我的代碼:

using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;
using System.IO;
using System.Threading;

namespace miniWebServer_csc360
{
    public partial class Form1 : Form
    {
        public static int requestCount = 0;
        public static Boolean singleUse = true;

        public static HttpListener listener = new HttpListener();

        //public static HttpListenerContext context;
        //public static HttpListenerResponse response;

        static Form1 thisform;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            thisform = this;
        }

        private void start_button_Click(object sender, EventArgs e)
        {
            Thread t = new Thread(serverThread);
            t.Start();
        }

        public static void serverThread()
        {
            if (singleUse)
            {
                listener.Prefixes.Add("http://10.211.55.4:69/");
                listener.Prefixes.Add("http://localhost:69/");

                singleUse = false;
            }

            if (listener.IsListening)
                listener.Stop();

            else
                listener.Start();

            if (listener.IsListening)
            {
                thisform.Invoke((MethodInvoker)delegate { thisform.status_label.Text 
                   = "Listening..."; });
                thisform.Invoke((MethodInvoker)delegate { thisform.start_button.Text 
                   = "Stop Server"; });
            }

            else
            {
                thisform.Invoke((MethodInvoker)delegate { thisform.status_label.Text 
                   = "Not listening..."; });
                thisform.Invoke((MethodInvoker)delegate { thisform.start_button.Text 
                   = "Start Server"; });
            }

            while (listener.IsListening)
            {
                try
                {
                    HttpListenerContext context = listener.GetContext();
                    HttpListenerResponse response = context.Response;

                    requestCount++;

                    thisform.Invoke((MethodInvoker)delegate { thisform.counter.Text 
                       = requestCount.ToString(); });

                    string page = Directory.GetCurrentDirectory() 
                        + context.Request.Url.LocalPath;

                    if (context.Request.Url.LocalPath != "/HEAD.html" 
                         && context.Request.Url.LocalPath != "/favicon.ico")
                    {
                        if (context.Request.Url.LocalPath == "/")
                        {
                            string path = context.Request.Url.LocalPath + "index.html";
                                 thisform.Invoke((MethodInvoker)delegate
                            {
                                thisform.request_Box.AppendText(path + "\r\n");
                            });
                        }

                        else
                        {
                            thisform.Invoke((MethodInvoker)delegate
                            {
                                thisform.request_Box.AppendText
                                  (context.Request.Url.LocalPath + "\r\n");
                            });
                        }
                    }

                    if (context.Request.Url.LocalPath == "/")
                        page += "index.html";

                    TextReader tr = new StreamReader(page);
                    string msg = tr.ReadToEnd();

                    byte[] buffer = Encoding.UTF8.GetBytes(msg);

                    if (context.Request.Url.LocalPath == "/header.html")
                    {
                        File.WriteAllText(Directory.GetCurrentDirectory() + "/HEAD.html",
                            context.Request.HttpMethod + " " +
                            context.Request.RawUrl + " HTTP/" 
                            + context.Request.ProtocolVersion + "\n"
                            + context.Request.Headers.ToString());
                    }

                    response.ContentLength64 = buffer.Length;
                    Stream st = response.OutputStream;
                    st.Write(buffer, 0, buffer.Length);

                    context.Response.Close();
                }
                catch (Exception error)
                {

                }
            }
        }
    }
}

提前致謝!

您必須將HttpListenerResponse.ContentType設置為圖像的適當MIME類型,例如“ image / gif”。

另外,示例代碼是通過TextReader讀取圖像並將其轉換為UTF8字節數組。 對於二進制文件,這是行不通的。 它會破壞字節。 您需要使用FileStream或任何其他方法讀取這些文件,這些方法將返回未修改的二進制文件內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM