How can I use an HttpListener when the URL does not end with "/"?

I want to listen to the url for some information. so my code is this:

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); } 

The URL that I want to add as a prefix does not end with "/". and if I add it to the end of the URL, then it is invalid and does not work.

so how can i listen to the url so that it doesn't end with "/" ??

+5
source share
1 answer

The URL you supply is a prefix, so the handler for the http: // xxx: 8086 prefix will match any requests starting with this line, including http: // xxx: 8086 / sms.html . For this reason, prefixes must end with "/", and it is described here: https://msdn.microsoft.com/en-us/library/system.net.httplistener(v=vs.110).aspx

If you want your listener to listen only to specific paths, you need to write some logic and check that the URL is in the request. To do this for each request, you must provide an httplistener callback and check the uri request.

Here's a small program that returns 200 OK for http: // localhost: 8086 / nisse.html and http: // localhost: 8086 / kalle.html , but 404 for all other URLs with the prefix 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(); } } } 
0
source

All Articles