語言

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

A Uri 代表與請求相關的資源。

例外狀況

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

範例

以下範例將 RequestUri 檢查該物業以確定最初申請的地點。

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

備註

注意事項

WebRequest、、HttpWebRequest、ServicePointWebClient、和 都已過時,你不應該用它們來做新開發。 請改用 HttpClient。

當在後代類別中覆寫時,該 RequestUri 屬性包含 Uri 該 Create 方法用來建立請求的實例。

Note

這 WebRequest 堂課就是一 abstract 堂課。 執行時實 WebRequest 例的實際行為由方法回傳 WebRequest.Create 的後代類別決定。 欲了解更多關於預設值與例外的資訊,請參閱後代類別的文件,如 HttpWebRequest 和 FileWebRequest。

給實施者的注意事項

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

適用於

另請參閱