HttpListenerRequest 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
描述 HttpListener 物件的連入 HTTP 要求。 此類別無法獲得繼承。
public ref class HttpListenerRequest sealed
public sealed class HttpListenerRequest
type HttpListenerRequest = class
Public NotInheritable Class HttpListenerRequest
- 繼承
-
HttpListenerRequest
範例
下列程式碼範例示範如何接收和回應 HttpListenerRequest 。
// 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
備註
當用戶端向物件處理 HttpListener 統一資源識別項 (URI) 提出要求時,會 HttpListener 提供 HttpListenerContext 物件,其中包含傳送者、要求和傳送給用戶端的回應相關資訊。 屬性 HttpListenerContext.Request 會傳 HttpListenerRequest 回描述要求的物件。
物件 HttpListenerRequest 包含要求的相關資訊,例如要求 HttpMethod 字串、 UserAgent 字串和要求本文資料, (請參閱 InputStream 屬性) 。
若要回復要求,您必須使用 Response 屬性取得相關聯的回應。
屬性
AcceptTypes |
取得用戶端接受的 MIME 類型。 |
ClientCertificateError |
取得錯誤碼,識別用戶端所提供之 X509Certificate 的問題。 |
ContentEncoding |
取得可用於隨要求所傳送資料的內容編碼方式。 |
ContentLength64 |
取得要求中所包含之主體資料的長度。 |
ContentType |
取得要求中所包含之主體資料的 MIME 類型。 |
Cookies |
取得隨要求傳送的 Cookie。 |
HasEntityBody |
取得 Boolean 值,指出要求是否有關聯的主體資料。 |
Headers |
取得在要求中所傳送之標頭名稱/值組的集合。 |
HttpMethod |
取得用戶端指定的 HTTP 方法。 |
InputStream |
取得包含用戶端所傳送之主體資料的資料流。 |
IsAuthenticated |
取得 Boolean 值,指出傳送這個要求的用戶端是否已經過驗證。 |
IsLocal |
取得 Boolean 值,指出要求是否從本機電腦送出。 |
IsSecureConnection |
取得 Boolean 值,指出用來傳送要求的 TCP 連線是否使用 Secure Sockets Layer (SSL) 通訊協定。 |
IsWebSocketRequest |
取得 Boolean 值,指出 TCP 連接是否為 WebSocket 要求。 |
KeepAlive |
取得 Boolean 值,指出用戶端是否要求持續性連線。 |
LocalEndPoint |
取得要求所導向到的伺服器 IP 位址和連接埠編號。 |
ProtocolVersion |
取得要求的用戶端所使用的 HTTP 版本。 |
QueryString |
取得要求中所包含的查詢字串。 |
RawUrl |
取得用戶端所要求的 URL 資訊 (不含主機和連接埠)。 |
RemoteEndPoint |
取得起始要求的用戶端 IP 位址和連接埠編號。 |
RequestTraceIdentifier |
取得連入之 HTTP 要求的要求識別項。 |
ServiceName |
取得用戶端透過要求傳送的服務提供者名稱 (SPN)。 |
TransportContext |
取得用戶端要求的 TransportContext。 |
Url |
取得用戶端所要求的 Uri 物件。 |
UrlReferrer |
取得用戶端用來參考伺服器之資源的統一資源識別元 (URI)。 |
UserAgent |
取得用戶端所表示的使用者代理程式。 |
UserHostAddress |
取得要求所導向到的伺服器 IP 位址和連接埠編號。 |
UserHostName |
取得 DNS 名稱和用戶端所指定的連接埠編號 (如果有提供的話)。 |
UserLanguages |
取得回應慣用的自然語言。 |
方法
BeginGetClientCertificate(AsyncCallback, Object) |
開始用戶端之 X.509 v.3 憑證的非同步要求。 |
EndGetClientCertificate(IAsyncResult) |
結束用戶端之 X.509 v.3 憑證的非同步要求。 |
Equals(Object) |
判斷指定的物件是否等於目前的物件。 (繼承來源 Object) |
GetClientCertificate() |
擷取用戶端的 X.509 v.3 憑證。 |
GetClientCertificateAsync() |
以非同步作業的方式,擷取用戶端的 X.509 v.3 憑證。 |
GetHashCode() |
做為預設雜湊函式。 (繼承來源 Object) |
GetType() |
取得目前執行個體的 Type。 (繼承來源 Object) |
MemberwiseClone() |
建立目前 Object 的淺層複製。 (繼承來源 Object) |
ToString() |
傳回代表目前物件的字串。 (繼承來源 Object) |