WebRequest.GetResponse 方法

定義

在子代類別中覆寫時,傳回對網際網路要求的回應。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

傳回

WebResponse

WebResponse,包含對網際網路要求的回應。A 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()

備註

GetResponse方法會將要求傳送至網際網路資源並傳回 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方法會提供的同步存取 WebResponseThe 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.CreateThe 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.

適用於

另請參閱