WebRequest.GetResponse 方法

定义

在后代类中重写时,返回对 Internet 请求的响应。

public:
 virtual System::Net::WebResponse ^ GetResponse();
public virtual System.Net.WebResponse GetResponse ();
abstract member GetResponse : unit -> System.Net.WebResponse
override this.GetResponse : unit -> System.Net.WebResponse
Public Overridable Function GetResponse () As WebResponse

返回

包含对 Internet 请求的响应的 WebResponse

例外

在子代类中未重写该方法时,将尝试访问该方法。

示例

以下示例将 Timeout 属性设置为 10000 毫秒。 如果超时期限在返回资源之前过期,则会引发 WebException

// Create a new WebRequest Object to the mentioned URL.
WebRequest^ myWebRequest = WebRequest::Create( "http://www.contoso.com" );
Console::WriteLine( "\nThe Timeout time of the request before setting is : {0} milliseconds", myWebRequest->Timeout );

// Set the 'Timeout' property in Milliseconds.
myWebRequest->Timeout = 10000;

// This request will throw a WebException if it reaches the timeout limit
// before it is able to fetch the resource.
WebResponse^ myWebResponse = myWebRequest->GetResponse();

         // Create a new WebRequest Object to the mentioned URL.
WebRequest myWebRequest=WebRequest.Create("http://www.contoso.com");
Console.WriteLine("\nThe Timeout time of the request before setting is : {0} milliseconds",myWebRequest.Timeout);

// Set the 'Timeout' property in Milliseconds.
myWebRequest.Timeout=10000;

// This request will throw a WebException if it reaches the timeout limit before it is able to fetch the resource.
         WebResponse myWebResponse=myWebRequest.GetResponse();

   ' Create a new WebRequest Object to the mentioned URL.
   Dim myWebRequest As WebRequest = WebRequest.Create("http://www.contoso.com")
   Console.WriteLine(ControlChars.Cr + "The Timeout time of the request before setting is : {0} milliseconds", myWebRequest.Timeout)

   ' Set the 'Timeout' property in Milliseconds.
myWebRequest.Timeout = 10000

  ' Assign the response object of 'WebRequest' to a 'WebResponse' variable.
   Dim myWebResponse As WebResponse = myWebRequest.GetResponse()

注解

谨慎

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

GetResponse 方法将请求发送到 Internet 资源,并返回 WebResponse 实例。 如果请求已通过调用 GetRequestStream发起,GetResponse 方法将完成请求并返回任何响应。

GetResponse 方法提供对 WebResponse的同步访问。 对于异步访问,请使用 BeginGetResponseEndGetResponse 方法。

注意

WebRequest 类是 abstract 类。 运行时 WebRequest 实例的实际行为由 WebRequest.Create 方法返回的后代类确定。 有关默认值和异常的详细信息,请参阅子代类的文档,例如 HttpWebRequestFileWebRequest

注意

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

适用于

另请参阅