我有以下 HTTP 侦听器方法,深受 MSDN 使用 HttpListener 类的示例的启发。我对编程相当陌生,我不知道从这里到哪里从我的 Main() 初始化它。有什么建议么?
public static void HttpListener(string[] prefixes)
{
if (prefixes == null || prefixes.Length == 0)
throw new ArgumentException("Prefixes needed");
HttpListener listener = new HttpListener();
foreach (string s in prefixes)
{
listener.Prefixes.Add(s);
}
listener.Start();
Console.WriteLine("Listening..");
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
HttpListenerResponse response = context.Response;
string responseString = "<HTML><BODY> Test </BODY></HTML>";
byte[] buffer = Encoding.UTF8.GetBytes(responseString);
response.ContentLength64 = buffer.Length;
Stream output = response.OutputStream;
output.Write(buffer, 0, buffer.Length);
output.Close();
listener.Stop();
}
您似乎已经删除了上面提到的评论MSDN HttpListener 类 http://msdn.microsoft.com/en-us/library/system.net.httplistener(v=vs.110).aspx page:
// URI 前缀是必需的,例如“http://contoso.com:8080/index/ http://contoso.com:8080/index/".
所以就这样称呼它:
public static void Main(string[] args)
{
HttpListener(new[] { "http://localhost/" });
}
但请注意,此示例将仅处理一个请求,然后退出。如果您的后续问题是“我怎样才能让它处理多个请求?”, see 使用 C# HttpListener 处理多个请求 https://stackoverflow.com/questions/9034721/handling-multiple-requests-with-c-sharp-httplistener.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)