3

I want to code a c# Http webserver. If the URL is requested I want to send the HTML page with CSS and JS to the client. How can I do that?

static void Main(string[] args)
        {
            HttpListener server = new HttpListener();  // this is the http server
            //server.Prefixes.Add("http://127.0.0.1/");  //we set a listening address here (localhost)
            server.Prefixes.Add("http://localhost:2002/");

            server.Start();   // and start the server

            Console.WriteLine("Server started...");

            while (true)
            {
                HttpListenerContext context = server.GetContext();
                HttpListenerResponse response = context.Response;

                byte[] buffer = Encoding.UTF8.GetBytes("<html></html>");

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

                context.Response.Close();
            }
        }

I want to send the full HTML CSS JS website to the client.

sry for my bad english.

2 Answers 2

4

As I just figured out, I can now fix my problem.

I played around with the Request URL / RawURL stuff in the HTTPListenerContext class.

static void Main(string[] args)
    {
        HttpListener server = new HttpListener();  // this is the http server
        //server.Prefixes.Add("http://127.0.0.1/");  //we set a listening address here (localhost)
        server.Prefixes.Add("http://localhost:2002/");

        server.Start();   // and start the server

        Console.WriteLine("Server started...");

        while (true)
        {
            HttpListenerContext context = server.GetContext();
            HttpListenerResponse response = context.Response;


            Console.WriteLine("URL: {0}", context.Request.Url.OriginalString);
            Console.WriteLine("Raw URL: {0}", context.Request.RawUrl);




            byte[] buffer = File.ReadAllBytes("." + context.Request.RawUrl.Replace("%20", " "));

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

            context.Response.Close();
        }
    }

I simply send only the files the client (User) is requesting. Thanks to my good HTML website, it automatically requests the Pictures and CSS and JS files. So when I go onto http://localhost:2002/ it automatically sends me the full website.

sry for my bad english

Sign up to request clarification or add additional context in comments.

Comments

0

If the client you mentioned is capable of consuming and evaluating JS and CSS then you can just include the javascript/CSS as inline or as a separate file as part of your response, as below..

byte[] buffer = Encoding.UTF8.GetBytes("<html>
<head>
    <title>Page title</title>
 <link rel='stylesheet' type='text/css' href='styles.css'> 
</head>
<body>
</body>
<script src='js/script.js'></script>
</html>");

4 Comments

But how to include CSS and JS also Images from file?
your client should probably be able to fetch, process and display content based on the CSS and JS files you refer in the HTML content that you throw out as a response. Can you explain a bit more about your 'client' that you have mentioned in your question? excerpt from your question.."If the URL is requested I want to send the HTML page with CSS and JS to the client"
Okay. The client is the user who want to visit my website. I don't want to use IIS to host my Websites, so I want to code my own hosting program. The user is accessing the HTTPListener (The websocket). Now my question is how can the user get the website. The website contains multiple external files like CSS / HTML and JS files and pictures. How to configure the HTTPListener that it is sending those files to the user who accessed the HTTPListener (websocket).
I think we are talking about the client as a software that processes and displays HTML, css, js,etc. Your user need to use some kind of software to see the web content. You can write your own code to throw the HTML, CSS,etc from your server to the requesting user's machine, but your user must be using some kind of software to browse the content you are throwing out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.