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
注釈
Caution
新しい開発には HttpListener クラスを使用しないことをお勧めします。
HttpListener は限られたサービスのみを受け取り、その動作とプロトコルのサポートはプラットフォームによって異なります。各プラットフォームでは異なる基になる実装が使用されるためです。 たとえば、WindowsではHTTP.sys上に構築されますが、Linux と macOS では、HTTPS をサポートせず、より狭い機能セットを提供するマネージド実装が使用されます。
代わりに ASP.NET Coreを使用します。これは、完全にサポートされているクロスプラットフォームの高パフォーマンスの HTTP サーバーを提供します。
クライアントが、 HttpListener オブジェクトによって処理される URI (Uniform Resource Identifier) に対して要求を行うと、 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 |
要求の送信に使用される TCP 接続が Secure Sockets Layer (SSL) プロトコルを使用しているかどうかを示す Boolean 値を取得します。 |
| IsWebSocketRequest |
TCP 接続が WebSocket 要求であったかどうかを示す Boolean 値を取得します。 |
| KeepAlive |
クライアントが永続的な接続を要求するかどうかを示す Boolean 値を取得します。 |
| LocalEndPoint |
要求の転送先のサーバー IP アドレスとポート番号を取得します。 |
| ProtocolVersion |
要求するクライアントによって使用される HTTP バージョンを取得します。 |
| QueryString |
要求に含まれるクエリ文字列を取得します。 |
| RawUrl |
クライアントによって要求された URL 情報 (ホストとポートを除く) を取得します。 |
| RemoteEndPoint |
要求の送信元のクライアント IP アドレスとポート番号を取得します。 |
| RequestTraceIdentifier |
受信 HTTP 要求の要求識別子を取得します。 |
| ServiceName |
クライアントが要求で送信したサービス プロバイダー名 (SPN) を取得します。 |
| TransportContext |
クライアント要求の TransportContext を取得します。 |
| Url |
クライアントによって要求された Uri オブジェクトを取得します。 |
| UrlReferrer |
クライアントをサーバーに参照したリソースの URI (Uniform Resource Identifier) を取得します。 |
| 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) |