我正在製作一個小程序來從我的網頁檢索郵件。從網頁捕捉郵件
在我的頁面我有一個2個文本框和1個「提交」按鈕的表單。
在我的c#中有代碼讓頁面在服務器上運行。 我怎樣才能使它當網頁的用戶按下一個按鈕,我可以從網頁,並顯示POST數據到一個Console.Writeline();
有人能向我解釋,我怎麼能做到這一點。
的代碼從我的服務器加載網頁:
static HttpListener _listener = new HttpListener();
static Stream _ouput;
public static void StartListening()
{
//create new Httplistener
string localPrefix = "http://localhost:8000/";
_listener.Prefixes.Add(prefix);
_listener.Prefixes.Add(localPrefix);
_listener.Start();
Console.WriteLine("Listening....");
}
public static void GetContext()
{
// De GetContext methode blokkeert terwijl die wacht op een aanvraag(request)
HttpListenerContext context = _listener.GetContext();
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
string html = Properties.Resources.index;
byte[] buffer = Encoding.UTF8.GetBytes(html);
response.ContentLength64 = buffer.Length;
_ouput = response.OutputStream;
_ouput.Write(buffer, 0, buffer.Length);
}
public static void StopListening()
{
_ouput.Close();
_listener.Close();
}
編輯 一些意見後,我有我的代碼來完成。一個小問題,我的html表單不提交表單的內容。
我怎樣才能讓我的JavaScript提交資料,我可以趕上它與我的C#代碼
這可能對您有所幫助:http://www.codeproject。com/Articles/599978/An-HttpListener-Server-for-Handling-AJAX-POST-Requ –
Thx用於鏈接,但是當我點擊我的網頁上的提交按鈕時,我的c#程序中沒有收到數據。 –
@HuyHoangPham你能幫我一下嗎 –