HttpWebRequest.GetResponse メソッド

定義

インターネット リソースからの応答を返します。

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

インターネット リソースからの応答を格納している WebResponse

例外

前回の BeginGetResponse(AsyncCallback, Object) への呼び出しでストリームが既に使用中です。

Method が GET または HEAD で、ContentLength が 0 以上であるか SendChunkedtrueです。

要求キャッシュの検証コントロールは、この要求に対する応答がキャッシュから提供されたことを示していますが、この要求にはサーバーに送信されるデータが含まれています。 データを送信する要求には、キャッシュを使用できません。 正しく実装されていないカスタム キャッシュの検証コントロールを使用すると、この例外が発生する場合があります。

Abort() は以前に呼び出されました。

  • または - 要求のタイムアウト期間の期限が切れました。

  • または - 要求の処理中にエラーが発生しました。

次のコード例では、要求の応答を取得します。

#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.
'...
'
'

注釈

このメソッドは GetResponseWebResponse インターネット リソースからの応答を含むオブジェクトを返します。 返される実際のインスタンスは、HTTP 固有の HttpWebResponseプロパティにアクセスするために、そのクラスに型キャストできます。

A ProtocolViolationException は、クラスに設定されたプロパティが競合している場合に HttpWebRequest スローされます。 この例外は、アプリケーションがプロパティとプロパティをContentLengthSendChunked設定し、HTTP GET 要求を送信した場合にtrue発生します。 この例外は、アプリケーションが HTTP 1.0 プロトコルのみをサポートするサーバーにチャンクを送信しようとした場合に発生します。このプロトコルはサポートされていません。 この例外は、アプリケーションがプロパティを設定 ContentLength せずにデータを送信しようとした場合、または SendChunked バッファリングが false 無効でキープアライブ接続 ( KeepAlive プロパティは true) にある場合に発生します。.

注意事項

ストリームを閉じて接続を Close 解放するには、メソッドを呼び出す必要があります。 これを行わないと、アプリケーションが接続を使い果たしてしまう可能性があります。

POST メソッドを使用する場合は、要求ストリームを取得し、投稿するデータを書き込み、ストリームを閉じる必要があります。 このメソッドは、コンテンツの投稿の待機をブロックします。タイムアウト セットがなく、コンテンツを提供しない場合、呼び出し元のスレッドは無期限にブロックされます。

注意

同じ応答オブジェクトを返す GetResponse 複数の呼び出し。要求は再発行されません。

注意

アプリケーションでは、特定の要求に対して同期メソッドと非同期メソッドを混在させることはできません。 メソッドを呼び出す GetRequestStream 場合は、メソッドを GetResponse 使用して応答を取得する必要があります。

注意

a WebException がスローされた場合は、例外の Response プロパティと Status プロパティを使用して、サーバーからの応答を特定します。

注意

このメンバーは、アプリケーションでネットワーク トレースが有効にされている場合にトレース情報を出力します。 詳細については、「.NET Frameworkのネットワーク トレース」を参照してください

注意

セキュリティ上の理由から、Cookie は既定で無効になっています。 Cookie を使用する場合は、プロパティを CookieContainer 使用して Cookie を有効にします。

適用対象

こちらもご覧ください