1. Create a console project and enter the following code:
Static void Main (string [] args)
{
If (! HttpListener. IsSupported)
{
Throw new System. InvalidOperationException ("version too low ");
}
String [] prefixes = new string [] {"http: // localhost: 50000 /"};
HttpListener listener = new HttpListener ();
// Add a prefix
Foreach (string s in prefixes)
{
Listener. Prefixes. Add (s );
}
// Start listening
Listener. Start ();
Console. WriteLine ("listening ,,");
While (true)
{
HttpListenerContext context = listener. GetContext ();
// Obtain the request object
HttpListenerRequest request = context. Request;
Console. WriteLine ("{0} {1} HTTP/1.1", request. HttpMethod, request. RawUrl );
Console. WriteLine ("Accept: {0}", string. Join (",", request. AcceptTypes ));
Console. WriteLine ("Accept-Language: {0}", string. Join (",", request. userages ));
Console. WriteLine ("User-Agent: {0}", request. UserAgent );
Console. WriteLine ("Accept-Encoding: {0}", request. Headers ["Accept-Encoding"]);
Console. WriteLine ("Connection: {0}", request. KeepAlive? "Keep-Alive": "close ");
Console. WriteLine ("Host: {0}", request. UserHostName );
Console. WriteLine ("Pragma: {0}", request. Headers ["Pragma"]);
// Obtain the response object
HttpListenerResponse response = context. Response;
String responseString = "Response. ContentLength64 = System. Text. Encoding. UTF8.GetByteCount (responseString );
Response. ContentType = "text/html; charset = UTF-8"; // set the output type
System. IO. Stream output = response. OutputStream;
System. IO. StreamWriter writer = new System. IO. StreamWriter (output );
Writer. Write (responseString );
Writer. Close ();
If (Console. KeyAvailable)
{
Break;
}
}
Listener. Stop ();
}
Static void Main (string [] args)
{
If (! HttpListener. IsSupported)
{
Throw new System. InvalidOperationException ("version too low ");
}
String [] prefixes = new string [] {"http: // localhost: 50000 /"};
HttpListener listener = new HttpListener ();
// Add a prefix
Foreach (string s in prefixes)
{
Listener. Prefixes. Add (s );
}
// Start listening
Listener. Start ();
Console. WriteLine ("listening ,,");
While (true)
{
HttpListenerContext context = listener. GetContext ();
// Obtain the request object
HttpListenerRequest request = context. Request;
Console. WriteLine ("{0} {1} HTTP/1.1", request. HttpMethod, request. RawUrl );
Console. WriteLine ("Accept: {0}", string. Join (",", request. AcceptTypes ));
Console. WriteLine ("Accept-Language: {0}", string. Join (",", request. userages ));
Console. WriteLine ("User-Agent: {0}", request. UserAgent );
Console. WriteLine ("Accept-Encoding: {0}", request. Headers ["Accept-Encoding"]);
Console. WriteLine ("Connection: {0}", request. KeepAlive? "Keep-Alive": "close ");
Console. WriteLine ("Host: {0}", request. UserHostName );
Console. WriteLine ("Pragma: {0}", request. Headers ["Pragma"]);
// Obtain the response object
HttpListenerResponse response = context. Response;
String responseString = "Response. ContentLength64 = System. Text. Encoding. UTF8.GetByteCount (responseString );
Response. ContentType = "text/html; charset = UTF-8"; // set the output type
System. IO. Stream output = response. OutputStream;
System. IO. StreamWriter writer = new System. IO. StreamWriter (output );
Writer. Write (responseString );
Writer. Close ();
If (Console. KeyAvailable)
{
Break;
}
}
Listener. Stop ();
}
2. Run the project
3. Results
From hi_dzj's column