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()

注解

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

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

注意

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

注意

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

适用于

另请参阅