2015-04-21 11 views
5

我想要聽取獲取一些信息的網址。所以我的代碼是這樣的:當URL沒有以「/」結尾時,我該如何使用HttpListener?

public static void SimpleListenerExample(string[] prefixes) 
{ 
    HttpListener listener = new HttpListener(); 
    // Add the prefixes. 
    foreach (string s in prefixes) 
    { 
     listener.Prefixes.Add(s); 
    } 
    listener.Start(); 
    //Console.WriteLine("Listening..."); 
    // Note: The GetContext method blocks while waiting for a request. 
    HttpListenerContext context = listener.GetContext(); 
    HttpListenerRequest request = context.Request; 
    // Obtain a response object. 
    HttpListenerResponse response = context.Response; 
    // Construct a response. 
    string responseString = "<HTML><BODY> Hello world!</BODY></HTML>"; 
    byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); 
    // Get a response stream and write the response to it. 
    response.ContentLength64 = buffer.Length; 
    System.IO.Stream output = response.OutputStream; 
    output.Write(buffer, 0, buffer.Length); 
    // You must close the output stream. 
    output.Close(); 
    listener.Stop(); 
} 

private void button2_Click(object sender, EventArgs e) 
{ 
    string[] test = { "http://xxx.xxxx.xxx.xxx:8086/sms.html" }; 
    SimpleListenerExample(test); 
} 

我想添加的URL作爲前綴,不以「/」結尾。如果我將它添加到url的末尾,它無效並且不起作用。

所以我怎麼能聽一個URL,它沒有以「/」結尾?

+0

在URL中,'''後面指定的值是查詢參數。例如'https://www.google.co.in/search?q = xxxxxx',其中'www.google.co.in/search'是一個網址,''q = xxxxx'是一個查詢參數。所以,如果你的url也包含這樣的值,那麼它意味着url包含查詢參數。 – Shell

+0

什麼URI不起作用? – Luaan

+2

你能發表一個前綴的例子嗎? – DanielV

回答

0

您提供的網址是前綴,因此前綴爲http://xxx:8086的處理程序將匹配以該字符串開頭的任何請求,包括http://xxx:8086/sms.html。這就是前綴必須以「/」結尾的原因,並且在這裏描述:https://msdn.microsoft.com/en-us/library/system.net.httplistener(v=vs.110).aspx

如果您希望偵聽器只偵聽特定路徑,則必須編寫一些邏輯,然後檢查URL中的URL請求。要爲每個請求執行此操作,您必須提供對httplistener的回調,並檢查請求的URI。

這裏有一個小程序,返回200 OK的http://localhost:8086/nisse.htmlhttp://localhost:8086/kalle.html,但是404的前綴 http://localhost:8086/其他所有網址。

class Program 
{ 
    private static HttpListener listener; 
    static string[] uris = 
     { 
      "http://localhost:8086/nisse.html", 
      "http://localhost:8086/kalle.html", 
     }; 

    private static void ListenerCallback(IAsyncResult result) 
    { 
     var context = listener.EndGetContext(result); 

     HttpListenerRequest request = context.Request;    
     HttpListenerResponse response = context.Response; 

     //Maybe not use exact string matching here 
     if (uris.Contains(request.Url.ToString())) 
     {     
      context.Response.StatusCode = 200; 
      context.Response.StatusDescription = "OK"; 
      string responseString = "<HTML><BODY> YOU ASKED FOR:" + request.Url + "</BODY></HTML>"; 
      byte[] buffer = System.Text.Encoding.UTF8.GetBytes(responseString); 
      response.ContentLength64 = buffer.Length; 
      System.IO.Stream output = response.OutputStream; 
      output.Write(buffer, 0, buffer.Length); 
      output.Close(); 
      context.Response.Close(); 
     } 
     else 
     { 
      context.Response.StatusCode = 404; 
      context.Response.StatusDescription = "NOT FOUND"; 
      context.Response.Close(); 
     } 
    } 

    private static void Main() 
    {     
     listener = new HttpListener(); 
     //Add the distinct prefixes, you may want ot parse this in a more elegant way 
     foreach (string s in uris.Select(u=>u.Substring(0,u.LastIndexOf("/")+1)).Distinct()) 
     { 
      listener.Prefixes.Add(s); 
     } 
     listener.Start(); 
     while (true) 
     { 
      var result = listener.BeginGetContext(ListenerCallback, listener); 
      result.AsyncWaitHandle.WaitOne(); 
     } 
    } 
} 
相關問題