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

返回

WebResponse

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

例外

该流已由对 BeginGetResponse(AsyncCallback, Object) 的上一个调用使用。

Method 为 GET 或 HEAD,且 ContentLength 大于或等于零,或 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.
'...
'
'

注解

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

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

注意

必须调用 Close 该方法以关闭流并释放连接。 未能执行此操作可能会导致应用程序耗尽连接。

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

备注

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

备注

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

备注

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

备注

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

备注

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

适用于

另请参阅