【问题标题】:How to create a simple local web page using C# windows forms如何使用 C# windows 窗体创建简单的本地网页
【发布时间】:2014-02-03 09:05:20
【问题描述】:

我希望使用 C# Windows 窗体应用程序或 C# 控制台应用程序创建一个简单的网页。

运行应用程序将开始在以下位置托管网页:

http://localhost:3070/somepage

我在 MSDN 上读过一些关于 using endpoints 的内容,但是我是自学成才的,这对我来说意义不大......

简而言之,这个程序在运行时会在 localhost:3070 的网页上显示一些文本。

抱歉问了这么一个含糊的问题,但是我搜索一个体面的教程的时间并没有产生任何可以理解的结果...

感谢您的宝贵时间!

【问题讨论】:

  • 您的问题是关于 asp.net 还是 Windows Forms?它们是完全不同的(Windows Forms 在你的标题中..因此我链接到HttpListener)..
  • 对不起,不应该标记 asp.net!不知道我在想什么。只是 Windows 窗体或 C# 控制台应用程序,而不是 ASP.net。

标签: c# localhost hosting endpoint


【解决方案1】:

微软发布了一个名为 OWIN 的开源项目,它与 Node 类似,但归根结底,它允许您在控制台应用程序中托管 Web 应用程序:

您可以在此处找到更多信息:

但如果你坚持创建你的个人听众,你可以在这里找到一些帮助:

【讨论】:

  • 每次看到微软和开源的一句话,我都需要重新阅读才能确定。 :>
  • 我还是习惯了邪恶的 M$ 把被诅咒的 IE6 塞到我脸上……现在看看它们 o_O
【解决方案2】:

? 2020 年更新:

原始答案在底部。

KestrelKatana 现在是一个东西,我强烈建议你看看这些东西以及OWIN


原答案:

你会想要考虑创建一个HttpListener,你可以给监听器添加前缀,例如Listener.Prefixes.Add("http://+:3070/"),它将它绑定到你想要的端口。

一个简单的控制台应用程序:计算发出的请求数

using System;
using System.Net;
using System.Text;

namespace TestServer
{
    class ServerMain
    {
        // To enable this so that it can be run in a non-administrator account:
        // Open an Administrator command prompt.
        // netsh http add urlacl http://+:8008/ user=Everyone listen=true
        
        const string Prefix = "http://+:3070/";
        static HttpListener Listener = null;
        static int RequestNumber = 0;
        static readonly DateTime StartupDate = DateTime.UtcNow;

        static void Main(string[] args)
        {
            if (!HttpListener.IsSupported)
            {
                Console.WriteLine("HttpListener is not supported on this platform.");
                return;
            }
            using (Listener = new HttpListener())
            {
                Listener.Prefixes.Add(Prefix);
                Listener.Start();
                // Begin waiting for requests.
                Listener.BeginGetContext(GetContextCallback, null);
                Console.WriteLine("Listening. Press Enter to stop.");
                Console.ReadLine();
                Listener.Stop();
            }
        }

        static void GetContextCallback(IAsyncResult ar)
        {
            int req = ++RequestNumber;

            // Get the context
            var context = Listener.EndGetContext(ar);

            // listen for the next request
            Listener.BeginGetContext(GetContextCallback, null);

            // get the request
            var NowTime = DateTime.UtcNow;

            Console.WriteLine("{0}: {1}", NowTime.ToString("R"), context.Request.RawUrl);

            var responseString = string.Format("<html><body>Your request, \"{0}\", was received at {1}.<br/>It is request #{2:N0} since {3}.",
                context.Request.RawUrl, NowTime.ToString("R"), req, StartupDate.ToString("R"));

            byte[] buffer = Encoding.UTF8.GetBytes(responseString);
            // and send it
            var response = context.Response;
            response.ContentType = "text/html";
            response.ContentLength64 = buffer.Length;
            response.StatusCode = 200;
            response.OutputStream.Write(buffer, 0, buffer.Length);
            response.OutputStream.Close();
        }
    }
}

为了获得额外积分,请尝试将其添加到您计算机上的服务中!

【讨论】:

  • 要添加@Oxymoron 帖子,为了让侦听器打开该端口,应用程序必须具有管理员权限,或者执行应用程序的用户必须允许 IP 和端口组合。或者,您可以使用netsh http add urlacl url=http://+:3070/ user="DOMAIN\User"(提示有一个代表所有人的用户帐户)。
  • 在代码的 cmets 中添加。谢谢@Nico。第 9、10、11 行
  • @Oxymoron 这个解决方案对我来说效果很好,但是我在修改它时遇到了一些困难......鉴于这个问题,这个答案很好,我的问题是当我尝试移植此页面时,以便可以在任何地方访问它。长话短说,我正在为我的一些 arduino 项目设置一个使用 HTTP 的串行通信协议。我希望能够获取任何序列信息,并使用此网页将其在线推送。有任何想法吗...?我尝试将 const string Prefix = "http://+:3070/"; 更改为我的 IP 地址,但我没有运气。
  • 如果您不介意,将 + 符号更改为您的 ip 不会为您做这件事。让我们开始一个新问题,我们将在那里讨论这个问题。
猜你喜欢
  • 2018-04-28
  • 2015-08-20
  • 2013-08-12
  • 2013-01-08
  • 1970-01-01
  • 2010-11-04
  • 2018-05-08
  • 1970-01-01
  • 2023-04-08
相关资源
最近更新 更多