HttpWebRequest.GetResponse 方法

定义

返回来自 Internet 资源的响应。

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

返回

包含来自 Internet 资源的响应的 WebResponse

例外

上一次调用 BeginGetResponse(AsyncCallback, Object)时,该流已使用。

-或-

TransferEncoding 设置为值,SendChunkedfalse

Method 为 GET 或 HEAD,并且 ContentLength 大于或等于零或 SendChunkedtrue

-或-

KeepAlive trueAllowWriteStreamBufferingfalseContentLength 为 -1,SendChunkedfalseMethod 为 POST 或 PUT。

-或-

HttpWebRequest 具有实体正文,但调用 GetResponse() 方法而不调用 GetRequestStream() 方法。

-或-

ContentLength 大于零,但应用程序不会写入所有承诺的数据。

请求缓存验证程序指示可从缓存中提供此请求的响应;但是,此请求包含要发送到服务器的数据。 发送数据的请求不得使用缓存。 如果使用未正确实现的自定义缓存验证程序,则可能会出现此异常。

以前调用 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.
'...
'
'

注解

谨慎

WebRequestHttpWebRequestServicePointWebClient 已过时,不应将其用于新开发。 请改用 HttpClient

GetResponse 方法返回一个 WebResponse 对象,该对象包含来自 Internet 资源的响应。 返回的实际实例是一个 HttpWebResponse,可以键入到该类以访问特定于 HTTP 的属性。

HttpWebRequest 类上设置的属性冲突时,会引发 ProtocolViolationException。 如果应用程序将 ContentLength 属性和 SendChunked 属性设置为 true,然后发送 HTTP GET 请求,则会发生此异常。 如果应用程序尝试将分块发送到仅支持 HTTP 1.0 协议的服务器(其中不支持此协议),则会发生此异常。 如果应用程序尝试在不设置 ContentLength 属性的情况下发送数据,或者在禁用缓冲和保持连接(KeepAlive 属性为 true)时 falseSendChunked,则会发生此异常.

谨慎

必须调用 Close 方法才能关闭流并释放连接。 否则,可能会导致应用程序耗尽连接。

使用 POST 方法时,必须获取请求流、写入要发布的数据以及关闭流。 此方法阻止等待内容发布;如果没有超时设置且未提供内容,则调用线程无限期地阻止。

注意

多次调用 GetResponse 返回相同的响应对象;请求未重新发出。

注意

应用程序无法混合特定请求的同步和异步方法。 如果调用 GetRequestStream 方法,则必须使用 GetResponse 方法来检索响应。

注意

如果引发 WebException,请使用异常的 ResponseStatus 属性来确定来自服务器的响应。

注意

在应用程序中启用网络跟踪时,此成员将输出跟踪信息。 有关详细信息,请参阅 .NET Framework中的 网络跟踪。

注意

出于安全原因,默认情况下会禁用 Cookie。 如果要使用 Cookie,请使用 CookieContainer 属性启用 Cookie。

适用于

另请参阅