积极答复者
HttpListener搭建的小服务器

问题
答案
-
您好,我把代码贴出来,您参考一下:
class HttpListenerDemo
{
// This example requires the System and System.Net namespaces.
public static void SimpleListenerExample(string[] prefixes)
{
if (!HttpListener.IsSupported)
{
Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
return;
}
// URI prefixes are required,
// for example "http://contoso.com:8080/index/".
if (prefixes == null || prefixes.Length == 0)
throw new ArgumentException("prefixes");// Create a listener.
HttpListener listener = new HttpListener();
// Add the prefixes.
foreach (string s in prefixes)
{
listener.Prefixes.Add(s);
}
listener.Start();
Console.WriteLine("Listening...");
string m_end = "";
// Note: The GetContext method blocks while waiting for a request.
while (m_end != "end")
{
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
string url = request.Url.ToString();
// Obtain a response object.
HttpListenerResponse response = context.Response;
// Construct a response.
string responseString = "<HTML><BODY> Hello world!<br>" + url + "</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();
if (url.IndexOf("end") != -1)
{
m_end = "end";
}
}
listener.Stop();
}public static void Main()
{
string[] m_Prefixs = new string[1];
m_Prefixs[0] = "http://localhost:8080/customerData/";
HttpListenerDemo.SimpleListenerExample(m_Prefixs);
}
}- 已建议为答案 Raymond TangModerator 2009年8月31日 2:26
- 已标记为答案 孟宪会Moderator 2011年3月12日 13:35
全部回复
-
您好,我把代码贴出来,您参考一下:
class HttpListenerDemo
{
// This example requires the System and System.Net namespaces.
public static void SimpleListenerExample(string[] prefixes)
{
if (!HttpListener.IsSupported)
{
Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
return;
}
// URI prefixes are required,
// for example "http://contoso.com:8080/index/".
if (prefixes == null || prefixes.Length == 0)
throw new ArgumentException("prefixes");// Create a listener.
HttpListener listener = new HttpListener();
// Add the prefixes.
foreach (string s in prefixes)
{
listener.Prefixes.Add(s);
}
listener.Start();
Console.WriteLine("Listening...");
string m_end = "";
// Note: The GetContext method blocks while waiting for a request.
while (m_end != "end")
{
HttpListenerContext context = listener.GetContext();
HttpListenerRequest request = context.Request;
string url = request.Url.ToString();
// Obtain a response object.
HttpListenerResponse response = context.Response;
// Construct a response.
string responseString = "<HTML><BODY> Hello world!<br>" + url + "</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();
if (url.IndexOf("end") != -1)
{
m_end = "end";
}
}
listener.Stop();
}public static void Main()
{
string[] m_Prefixs = new string[1];
m_Prefixs[0] = "http://localhost:8080/customerData/";
HttpListenerDemo.SimpleListenerExample(m_Prefixs);
}
}- 已建议为答案 Raymond TangModerator 2009年8月31日 2:26
- 已标记为答案 孟宪会Moderator 2011年3月12日 13:35