次の方法で共有


HttpWebRequest.GetResponse メソッド

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

Overrides Public Function GetResponse() As WebResponse
[C#]
public override WebResponse GetResponse();
[C++]
public: WebResponse* GetResponse();
[JScript]
public override function GetResponse() : WebResponse;

戻り値

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

例外

例外の種類 条件
InvalidOperationException ストリームが、 BeginGetResponse への前回の呼び出しで既に使用されています。

または

TransferEncoding に値が設定され、 SendChunkedfalse です。

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

または

KeepAlivetrue で、 AllowWriteStreamBufferingfalse で、 ContentLength は -1 で、 SendChunkedfalse で、 Method は POST または PUT です。

WebException Abort は既に呼び出されました。

または

要求のタイムアウト時間が経過しました。

または

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

解説

GetResponse メソッドは、インターネット リソースからの応答を格納している WebResponse インスタンスを返します。返された実際のインスタンスは HttpWebResponse のインスタンスであり、HTTP 固有のプロパティにアクセスするクラスに型キャストできます。

POST メソッドを使用する場合は、要求ストリームを取得し、ポストするデータを書き込んだ後、要求ストリームを閉じる必要があります。このメソッドは、コンテンツがポストされるまで待機します。したがって、タイムアウトが設定されておらず、コンテンツも提供されない場合、アプリケーションは無制限に待機することになります。

注意    Close メソッドを呼び出して、ストリームを閉じて接続を解放する必要があります。この操作を行わないと、アプリケーションで接続が不足することがあります。

使用例

[Visual Basic, C#] 次に示すのは、要求への応答を取得するコード例です。

 
Imports System
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 'Main
End Class 'Test
'
'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>
'
'

[C#] 
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>

*/

[C++, JScript] C++ および JScript のサンプルはありません。Visual Basic および C# のサンプルを表示するには、このページの左上隅にある言語のフィルタ ボタン 言語のフィルタ をクリックします。

必要条件

プラットフォーム: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 ファミリ, .NET Compact Framework - Windows CE .NET, Common Language Infrastructure (CLI) Standard

参照

HttpWebRequest クラス | HttpWebRequest メンバ | System.Net 名前空間 | Timeout