2014-01-15 51 views
4

我正在尋找使用C#Windows窗體應用程序或C#控制檯應用程序創建簡單的網頁。如何使用C#窗體創建簡單的本地網頁

運行該應用程序將開始在託管網頁:

http://localhost:3070/somepage 

我已閱讀MSDN上關於using endpoints一點點,但是是自學成才的,這不是做一噸的意義,我...

總之,這個程序在運行時會在localhost:3070的網頁上顯示一些文本。

對不起,這種含糊不清的問題,但我尋找一個體面的教程小時(S)沒有取得任何可理解的結果...

感謝您的時間!

+1

'HttpListener':http://msdn.microsoft.com/en-us/library/system.net.httplistener(v=vs.110).aspx –

+0

http://www.asp.net/web-表單 – user2930100

+1

是你關於asp.net或Windows Forms的問題嗎?他們是完全不同的(Windows窗體是在你的標題..因此,爲什麼我鏈接到'HttpListener').. –

回答

5

你會想看看創造一個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(); 
     } 
    } 
} 

而對於額外的信用,嘗試將其添加到您的計算機上的服務!

+0

要添加@Oxymoron文章,爲了監聽器必須以管理員權限打開該端口,或者執行應用程序的用戶必須允許IP和端口組合。或者,您可以使用'netsh http add urlacl url = http:// +:3070/user =「DOMAIN \ User」'(提示有一個表示每個人的用戶帳戶)。在代碼的評論中添加了 – Nico

+0

。謝謝@尼科。第9,10,11行 – Oxymoron

+0

@Oxymoron這個解決方案對我來說真的很好,但是我有一些困難,用它來修改東西......給出這個問題,這個答案很好,我的問題是當我試圖端口轉發此頁面,以便可以在任何地方訪問它。長話短說,我正在使用HTTP爲我的一些arduino項目建立一個串行通信協議。我希望能夠採取任何串行信息,並使用此網頁在線推送。有任何想法嗎...?我試着改變'const string Prefix =「http:// +:3070 /」;'到我的IP地址,我沒有運氣。 – JesterBaze

4

微軟Relased稱爲OWIN它的一個開源項目是simlar到節點,但底線是讓您的主機在一個控制檯應用程序的Web應用程序:

您可以在這裏找到更多的信息:

但是,如果你在創建自己的聽衆堅持,你可以找到一些幫助在這裏:

+2

每當我看到微軟和開放源碼在一個句子中,我需要重新讀一遍。 :> – PTwr

+0

我知道這很難相信:)... – Dalorzo

+1

我還是習慣了邪惡的M $把詛咒的IE6推到我的臉上......現在看看他們o_O – PTwr

相關問題