WebRequest.GetResponse 方法

定义

当在子类中被重写时,返回对 Internet 请求的响应。When overridden in a descendant class, returns a response to an Internet request.

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 请求的响应的 WebResponseA WebResponse containing the response to the Internet request.

异常

当未在子类中重写该方法时,试图访问该方法。Any attempt is made to access the method, when the method is not overridden in a descendant class.

示例

下面的示例将 Timeout 属性设置为10000毫秒。The following example sets the Timeout property to 10000 milliseconds. 如果超时期限在返回资源之前过期,则会引发 WebExceptionIf the timeout period expires before the resource can be returned, a WebException is thrown.

// 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 实例。The GetResponse method sends a request to an Internet resource and returns a WebResponse instance. 如果请求已通过调用 GetRequestStream启动,则 GetResponse 方法完成请求并返回任何响应。If the request has already been initiated by a call to GetRequestStream, the GetResponse method completes the request and returns any response.

GetResponse 方法提供对 WebResponse的同步访问。The GetResponse method provides synchronous access to the WebResponse. 对于异步访问,请使用 BeginGetResponseEndGetResponse 方法。For asynchronous access, use the BeginGetResponse and EndGetResponse methods.

备注

WebRequest 类是一个 abstract 类。The WebRequest class is an abstract class. 运行时 WebRequest 实例的实际行为取决于 WebRequest.Create 方法返回的子类。The actual behavior of WebRequest instances at run time is determined by the descendant class returned by the WebRequest.Create method. 有关默认值和异常的详细信息,请参阅子代类的文档,例如 HttpWebRequestFileWebRequestFor more information about default values and exceptions, see the documentation for the descendant classes, such as HttpWebRequest and FileWebRequest.

备注

如果引发了 WebException,则使用该异常的 ResponseStatus 属性来确定来自服务器的响应。If a WebException is thrown, use the Response and Status properties of the exception to determine the response from the server.

适用于

另请参阅