HttpWebRequest.GetResponse メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
インターネット リソースからの応答を返します。
public:
override System::Net::WebResponse ^ GetResponse();
public override System.Net.WebResponse GetResponse ();
override this.GetResponse : unit -> System.Net.WebResponse
Public Overrides Function GetResponse () As WebResponse
戻り値
インターネット リソースからの応答を格納している WebResponse。
例外
前回の BeginGetResponse(AsyncCallback, Object) への呼び出しでストリームが既に使用中です。
- または -
TransferEncoding が値に設定されており、SendChunked が false
です。
Method が GET または HEAD で、ContentLength が 0 以上であるか SendChunked が true
です。
- または -
KeepAlive が true
、AllowWriteStreamBuffering が false
、ContentLength が -1、SendChunked が false
で、Method が POST か PUT です。
- または -
HttpWebRequest にはエンティティ本体がありますが、GetRequestStream() メソッドを呼び出さずに GetResponse() メソッドが呼び出されています。
- または -
ContentLength が 0 よりも大きい値に設定されていますが、アプリケーションは保証されたデータをすべては書き込みません。
要求キャッシュの検証コントロールは、この要求に対する応答がキャッシュから提供されたことを示していますが、この要求にはサーバーに送信されるデータが含まれています。 データを送信する要求には、キャッシュを使用できません。 正しく実装されていないカスタム キャッシュの検証コントロールを使用すると、この例外が発生する場合があります。
例
次のコード例では、要求の応答を取得します。
#using <System.dll>
using namespace System;
using namespace System::Net;
using namespace System::Text;
using namespace System::IO;
// Specify the URL to receive the request.
int main()
{
array<String^>^args = Environment::GetCommandLineArgs();
HttpWebRequest^ request = dynamic_cast<HttpWebRequest^>(WebRequest::Create(args[1]));
// Set some reasonable limits on resources used by this request
request->MaximumAutomaticRedirections = 4;
request->MaximumResponseHeadersLength = 4;
// Set credentials to use for this request.
request->Credentials = CredentialCache::DefaultCredentials;
HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());
Console::WriteLine("Content length is {0}", response->ContentLength);
Console::WriteLine("Content type is {0}", response->ContentType);
// Get the stream associated with the response.
Stream^ receiveStream = response->GetResponseStream();
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader^ readStream = gcnew StreamReader(receiveStream, Encoding::UTF8);
Console::WriteLine("Response stream received.");
Console::WriteLine(readStream->ReadToEnd());
response->Close();
readStream->Close();
}
/*
The output from this example will vary depending on the value passed into Main
but will be similar to the following:
Content length is 1542
Content type is text/html; charset=utf-8
Response stream received.
<html>
...
</html>
*/
using System;
using System.Net;
using System.Text;
using System.IO;
public class Test
{
// Specify the URL to receive the request.
public static void Main (string[] args)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(args[0]);
// Set some reasonable limits on resources used by this request
request.MaximumAutomaticRedirections = 4;
request.MaximumResponseHeadersLength = 4;
// Set credentials to use for this request.
request.Credentials = CredentialCache.DefaultCredentials;
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Console.WriteLine("Content length is {0}", response.ContentLength);
Console.WriteLine("Content type is {0}", response.ContentType);
// Get the stream associated with the response.
Stream receiveStream = response.GetResponseStream();
// Pipes the stream to a higher level stream reader with the required encoding format.
StreamReader readStream = new StreamReader(receiveStream, Encoding.UTF8);
Console.WriteLine("Response stream received.");
Console.WriteLine(readStream.ReadToEnd());
response.Close();
readStream.Close();
}
}
/*
The output from this example will vary depending on the value passed into Main
but will be similar to the following:
Content length is 1542
Content type is text/html; charset=utf-8
Response stream received.
<html>
...
</html>
*/
Imports System.Net
Imports System.Text
Imports System.IO
Public Class Test
' Specify the URL to receive the request.
Public Shared Sub Main(ByVal args() As String)
Dim request As HttpWebRequest = CType(WebRequest.Create(args(0)), HttpWebRequest)
' Set some reasonable limits on resources used by this request
request.MaximumAutomaticRedirections = 4
request.MaximumResponseHeadersLength = 4
' Set credentials to use for this request.
request.Credentials = CredentialCache.DefaultCredentials
Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
Console.WriteLine("Content length is {0}", response.ContentLength)
Console.WriteLine("Content type is {0}", response.ContentType)
' Get the stream associated with the response.
Dim receiveStream As Stream = response.GetResponseStream()
' Pipes the stream to a higher level stream reader with the required encoding format.
Dim readStream As New StreamReader(receiveStream, Encoding.UTF8)
Console.WriteLine("Response stream received.")
Console.WriteLine(readStream.ReadToEnd())
response.Close()
readStream.Close()
End Sub
End Class
'
'The output from this example will vary depending on the value passed into Main
'but will be similar to the following:
'
'Content length is 1542
'Content type is text/html; charset=utf-8
'Response stream received.
'...
'
'
注釈
メソッドは GetResponse 、 WebResponse インターネット リソースからの応答を含む オブジェクトを返します。 返される実際のインスタンスは であり HttpWebResponse、そのクラスに型キャストして HTTP 固有のプロパティにアクセスできます。
ProtocolViolationExceptionクラスに設定されたプロパティが競合している場合、いくつかのケースで がHttpWebRequestスローされます。 この例外は、アプリケーションが プロパティと プロパティを ContentLengthSendChunked に設定し、HTTP GET 要求を送信した場合に true
発生します。 この例外は、アプリケーションが HTTP 1.0 プロトコルのみをサポートするサーバーにチャンクを送信しようとした場合に発生します。このプロトコルはサポートされていません。 この例外は、アプリケーションが プロパティを設定ContentLengthせずにデータを送信しようとした場合、または SendChunkedfalse
バッファリングが無効になっているときとキープアライブ接続 (プロパティが true
) で である場合にKeepAlive発生します。.
注意事項
ストリームを閉じて接続を Close 解放するには、 メソッドを呼び出す必要があります。 これを行わないと、アプリケーションの接続が不足する可能性があります。
POST メソッドを使用する場合は、要求ストリームを取得し、投稿するデータを書き込み、ストリームを閉じる必要があります。 このメソッドは、コンテンツが投稿されるのを待つのをブロックします。タイムアウトセットがなく、コンテンツを提供しない場合、呼び出し元のスレッドは無期限にブロックします。
注意
同じ応答オブジェクトを返す GetResponse 複数の呼び出し。要求は再発行されません。
注意
アプリケーションでは、特定の要求に対して同期メソッドと非同期メソッドを混在させることはできません。 メソッドを呼び出す GetRequestStream 場合は、 メソッドを GetResponse 使用して応答を取得する必要があります。
注意
WebExceptionがスローされた場合は、例外の プロパティと Status プロパティを使用Responseして、サーバーからの応答を確認します。
Note
このメンバーは、アプリケーションでネットワーク トレースが有効にされている場合にトレース情報を出力します。 詳細については、「 .NET Framework のネットワーク トレース」を参照してください。
注意
セキュリティ上の理由から、Cookie は既定で無効になっています。 Cookie を使用する場合は、 プロパティを CookieContainer 使用して Cookie を有効にします。
適用対象
こちらもご覧ください
.NET
フィードバック
https://aka.ms/ContentUserFeedback」を参照してください。
以下は間もなく提供いたします。2024 年を通じて、コンテンツのフィードバック メカニズムとして GitHub の issue を段階的に廃止し、新しいフィードバック システムに置き換えます。 詳細については、「フィードバックの送信と表示