gpt4 book ai didi

c# - 当 url 没有以 "/"结尾时,如何使用 HttpListener?

转载 作者:太空狗 更新时间:2023-10-29 23:50:45 24 4
gpt4 key购买 nike

我想听一个 url 来获取一些信息。所以我的代码是这样的:

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 呢??

最佳答案

您提供的 url 是一个前缀,因此前缀的处理程序 http://xxx:8086将匹配任何以该字符串开头的请求,包括 http://xxx:8086/sms.html .这就是前缀必须以“/”结尾的原因,此处对其进行了描述:https://msdn.microsoft.com/en-us/library/system.net.httplistener(v=vs.110).aspx

如果您希望您的监听器只监听特定路径,您必须编写一些逻辑,并检查请求中的 URL。要针对每个请求执行此操作,您必须向 httplistener 提供回调,并检查请求 uri。

这是一个为 http://localhost:8086/nisse.html 返回 200 OK 的小程序和 http://localhost:8086/kalle.html , 但对于所有其他带有前缀的 url 都是 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();
}
}
}

关于c# - 当 url 没有以 "/"结尾时,如何使用 HttpListener?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29764502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com