2015-06-05 52 views
2

我正在製作一個小程序來從我的網頁檢索郵件。從網頁捕捉郵件

在我的頁面我有一個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#代碼

+0

這可能對您有所幫助:http://www.codeproject。com/Articles/599978/An-HttpListener-Server-for-Handling-AJAX-POST-Requ –

+0

Thx用於鏈接,但是當我點擊我的網頁上的提交按鈕時,我的c#程序中沒有收到數據。 –

+0

@HuyHoangPham你能幫我一下嗎 –

回答

3

好吧,首先,你可以用你的代碼來捕獲數據。 但是你的htmlListener存在一個小問題。它是一個時間函數,我的意思是你只能連接一次。

你可以做這樣的事情:

public bool Listening = true; 

public static void GetContext() 
{ 
    while (Listening) 
    { 
     // 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); 
    } 
} 

然後,如果你,如果你使用你的數據,並使用一個嘗試捕捉,那麼你可以設置布爾假,如果有一個exceptioon還是可以繼續功能,所以它會走出循環。

您可以讓您的數據是這樣的:

HttpListenerContext httpContext = _listener.GetContext(); 
HttpListenerRequest httpRequest = context.Request; 

if (request.HttpMethod == "POST") 
{ 

} 

而對於JavaScript部分,只要保持好和容易。我用過這樣的東西:

<html> 
    <head> 
     <script language="javascript"> 
      var delayrec = {}; 
      function delay(callback, id, calldelay) { 
       clearTimeout(delayrec[id]); 
       delayrec[id] = setTimeout(callback, calldelay); 
      } 

      function verzenden(event) { 
       var keycode = document.getElementById("form_keycode"); 

       if (event.keyCode == 125 || event.keyCode == 113) {     
        keycode.value = 125; 
       } 
       else if (event.keyCode == 126 || event.keyCode == 115) {      
        keycode.value = 126; 
       } 

       delay(function() { 
        document.myform.submit(); 
       }, "submitlocatie", 500); 
      } 

      function load() { 
       var locatie = document.getElementById("locatie"); 
       var bonregel = document.getElementById("bonregel"); 
       if (locatie.value == "") 
        locatie.focus(); 
       else 
        bonregel.focus(); 
      } 

      $(document).keydown(function (eventObject) { 
       alert(eventObject.keyCode); 
      }); 



     </script> 
    </head> 
    <body onload="load()"> 
     <div class="wrapper"> 
      <fieldset> 
       <legend> test</legend><br /> 
       <form name="myform" action="/" method="post"> 
        <input type="hidden" id="keycode" name="keycode" /> 
        <table> 
         <tr> 
          <td>Text1:</td> 
          <td><input type="text" id="textfield1" name="textfield1" onkeyup="verzenden(event)" /></td> 
         </tr> 
         <tr> 
          <td>Text2:</td> 
          <td><input type="text" id="textfield2" name="textfield2" onkeyup="verzenden(event)" /></td> 
         </tr> 
        </table> 
       </form> 
       <p id="errorSection">{errorSection}</p> 
      </fieldset> 
     </div> 
    </body> 
</html> 

我使用keyCode事件來發送數據,並取消發送。

我希望這會有所幫助,祝您好運

+1

老年人瀏覽器謝謝男人!這有助於非常frick! –

1

您可以使用HttpListenerRequest和檢查方法類型爲POST

HttpListenerContext context = _listener.GetContext(); 
HttpListenerRequest request = context.Request; 

if (request.HttpMethod == "POST") 
{ 

} 

你可以閱讀和如圖所示顯示POST數據here

1

在客戶端使用Jquery Ajax調用。假設您的輸入字段ID爲input1和input2,並且提交按鈕具有id = submitBtn。你可以這樣做:

$(window).load(function() {   // after browser has loaded win 
    $('#submitBtn').click(function() { // add button click handler 
     var inp1 = $('#input1').val(); // get value from input 1 
     var inp2 = $('#input2').val(); // get value from input 2 
       $.ajax({    // send ajax POST request on server 
        url: "http://localhost:8000/", 
        type: "POST", 
        data: {input1:inp1 , input2:inp2}, 
       }).done(function() { 
        ...your logic on success 
        ...for example - location.reload(); 
       }).fail(function (err) { 
        alert(err.statusText); 
       }); 
    }); 
}); 

不要忘了將jquery庫包含到你的html文檔中。

smth like <script src=".../jquery.min.js"> 
+0

我認爲普通的JS會更容易在mu邊 –

+0

你知道,創建JQuery的主要原因之一是因爲它更容易!嘗試一下,你會沒事 – Fabjan

+0

是的,我明白,但我必須使用像ie5 –