HttpListener.GetContext 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
等待传入的请求,接收到请求时返回。
public:
System::Net::HttpListenerContext ^ GetContext();
public System.Net.HttpListenerContext GetContext ();
member this.GetContext : unit -> System.Net.HttpListenerContext
Public Function GetContext () As HttpListenerContext
返回
表示客户端请求的 HttpListenerContext 对象。
例外
Win32 函数调用失败。 检查异常的 ErrorCode 属性以确定导致异常的原因。
此对象已关闭。
示例
下面的代码示例演示如何调用此方法。
// 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...");
// 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();
}
Public Shared Sub SimpleListenerExample(prefixes As String())
If Not HttpListener.IsSupported Then
Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.")
Return
End If
' URI prefixes are required,
' for example "http://contoso.com:8080/index/".
If prefixes Is Nothing Or prefixes.Length = 0 Then
Throw New ArgumentException("prefixes")
End If
' Create a listener
Dim listener = New HttpListener()
For Each s As String In prefixes
listener.Prefixes.Add(s)
Next
listener.Start()
Console.WriteLine("Listening...")
' Note: The GetContext method blocks while waiting for a request.
Dim context As HttpListenerContext = listener.GetContext()
Console.WriteLine("Listening...")
' Obtain a response object
Dim request As HttpListenerRequest = context.Request
' Construct a response.
Dim response As HttpListenerResponse = context.Response
Dim responseString As String = "<HTML><BODY> Hello world!</BODY></HTML>"
Dim buffer As Byte() = System.Text.Encoding.UTF8.GetBytes(responseString)
' Get a response stream and write the response to it.
response.ContentLength64 = buffer.Length
Dim output As System.IO.Stream = response.OutputStream
output.Write(buffer, 0, buffer.Length)
'You must close the output stream.
output.Close()
listener.Stop()
End Sub
注解
在调用此方法之前,必须调用 Start 方法,并通过向 属性返回Prefixes的 添加 URI 字符串来添加至少一个 URI 前缀以HttpListenerPrefixCollection侦听。 有关前缀的详细说明,请参阅 HttpListener 类概述。
此方法在等待传入请求时阻止。 如果要在单独的线程) (异步处理传入请求,以便应用程序不会阻止,请使用 BeginGetContext 方法。
调用方说明
当你在应用程序中启用网络跟踪后,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework 中的网络跟踪。