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. 如果超时期限在可以返回资源之前过期, WebException则会引发。If 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()

注解

方法向 Internet 资源发送请求并返回一个WebResponse实例。 GetResponseThe 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.

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

备注

类是一个abstract类。 WebRequestThe 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. 有关默认值和异常的详细信息,请参阅子代类的文档,例如HttpWebRequest和。 FileWebRequestFor 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.

适用于

另请参阅