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

- 或 -

KeepAlivetrueAllowWriteStreamBufferingfalseContentLength 为 -1,SendChunkedfalseMethod 为 POST 或 PUT。

- 或 -

HttpWebRequest 具有实体主体,但在不调用 GetRequestStream() 方法的情况下调用了 GetResponse() 方法。

- 或 -

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

注解

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

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

注意

必须调用 Close 方法来关闭流并释放连接。 否则可能会导致应用程序连接不足。

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

注意

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

注意

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

注意

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

备注

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

注意

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

适用于

另请参阅