共用方式為


WebRequest.RequestUri 屬性

定義

在子代類別中覆寫時,取得與要求相關聯的因特網資源 URI。

public:
 abstract property Uri ^ RequestUri { Uri ^ get(); };
public:
 virtual property Uri ^ RequestUri { Uri ^ get(); };
public abstract Uri RequestUri { get; }
public virtual Uri RequestUri { get; }
member this.RequestUri : Uri
Public MustOverride ReadOnly Property RequestUri As Uri
Public Overridable ReadOnly Property RequestUri As Uri

屬性值

Uri

Uri,表示與要求相關聯的資源。

例外狀況

當屬性未在子系類別中覆寫時,會嘗試取得或設定 屬性。

範例

下列範例會檢查 RequestUri 屬性,以判斷原本要求的網站。

// Create a new WebRequest object to the mentioned URL.
WebRequest^ myWebRequest = WebRequest::Create( "http://www.contoso.com" );
Console::WriteLine( "\nThe Uri requested is {0}", myWebRequest->RequestUri);
// Assign the response object of 'WebRequest' to a 'WebResponse' variable.
WebResponse^ myWebResponse = myWebRequest->GetResponse();
      // Get the stream containing content returned by the server.
Stream^ streamResponse = myWebResponse->GetResponseStream();
      Console::WriteLine("\nThe Uri that responded to the request is '{0}'",myWebResponse->ResponseUri);
StreamReader^ reader = gcnew StreamReader(streamResponse);
      // Read the content.
String^ responseFromServer = reader->ReadToEnd();
// Display the content.
Console::WriteLine("\nThe HTML Contents received:");
Console::WriteLine (responseFromServer);
// Cleanup the streams and the response.
reader->Close();
streamResponse->Close();
myWebResponse->Close();
      // Create a new WebRequest Object to the mentioned URL.
      WebRequest myWebRequest=WebRequest.Create("http://www.contoso.com");
      Console.WriteLine("\nThe Uri that was requested is {0}",myWebRequest.RequestUri);
      // Assign the response object of 'WebRequest' to a 'WebResponse' variable.
      WebResponse myWebResponse=myWebRequest.GetResponse();
      // Get the stream containing content returned by the server.
      Stream streamResponse=myWebResponse.GetResponseStream();
      Console.WriteLine("\nThe Uri that responded to the WebRequest is '{0}'",myWebResponse.ResponseUri);
StreamReader reader = new StreamReader (streamResponse);
      // Read the content.
string responseFromServer = reader.ReadToEnd ();
// Display the content.
Console.WriteLine("\nThe HTML Contents received:");
Console.WriteLine (responseFromServer);
// Cleanup the streams and the response.
reader.Close ();
streamResponse.Close ();
myWebResponse.Close ();
' Create a new WebRequest Object to the mentioned URL.
Dim myWebRequest As WebRequest = WebRequest.Create("http://www.contoso.com")
Console.WriteLine(ControlChars.Cr + ControlChars.Lf +"The Uri that was requested is {0}", myWebRequest.RequestUri)
' Assign the response object of 'WebRequest' to a 'WebResponse' variable.
Dim myWebResponse As WebResponse = myWebRequest.GetResponse()
      ' Get the stream containing content returned by the server.
Dim streamResponse As Stream = myWebResponse.GetResponseStream()
Console.WriteLine(ControlChars.Cr + ControlChars.Lf + "The Uri that responded to the request is {0}", myWebResponse.ResponseUri)
' Print the HTML contents of the page to the console. 
Dim reader As New StreamReader(streamResponse)
      ' Read the content.
      Dim responseFRomServer As String = reader.ReadToEnd()
' Display the content.
Console.WriteLine(ControlChars.Cr + ControlChars.Lf +"The HTML Contents received:")
Console.WriteLine (responseFromServer)
' Cleanup the streams and the response.
reader.Close ()
streamResponse.Close ()
myWebResponse.Close ()

備註

謹慎

WebRequestHttpWebRequestServicePointWebClient 已經過時,您不應該將它們用於新的開發。 請改用 HttpClient

在子系類別中覆寫時,RequestUri 屬性會包含 Create 方法用來建立要求的 Uri 實例。

注意

WebRequest 類別是 abstract 類別。 運行時間 WebRequest 實例的實際行為是由 WebRequest.Create 方法所傳回的子代類別所決定。 如需預設值和例外狀況的詳細資訊,請參閱子代類別的檔,例如 HttpWebRequestFileWebRequest

給實施者的注意事項

RequestUri 必須包含傳遞至 Create(Uri) 方法的原始 Uri 實例。 如果通訊協議能夠將要求重新導向至不同的 URI 以服務要求,則子系必須提供 屬性以包含實際服務要求的 URI

適用於

另請參閱