WebRequest 類別

定義

對統一資源識別元 (URI) 提出要求。 這是 abstract 類別。

public ref class WebRequest abstract
public ref class WebRequest abstract : MarshalByRefObject, System::Runtime::Serialization::ISerializable
public abstract class WebRequest
public abstract class WebRequest : MarshalByRefObject, System.Runtime.Serialization.ISerializable
[System.Serializable]
public abstract class WebRequest : MarshalByRefObject, System.Runtime.Serialization.ISerializable
type WebRequest = class
type WebRequest = class
    inherit MarshalByRefObject
    interface ISerializable
[<System.Serializable>]
type WebRequest = class
    inherit MarshalByRefObject
    interface ISerializable
Public MustInherit Class WebRequest
Public MustInherit Class WebRequest
Inherits MarshalByRefObject
Implements ISerializable
繼承
WebRequest
繼承
衍生
屬性
實作

範例

下列範例示範如何建立 WebRequest 實例並傳回回應。

#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Net;
using namespace System::Text;
int main()
{
   
   // Create a request for the URL.   
   WebRequest^ request = WebRequest::Create( "http://www.contoso.com/default.html" );
   
   // If required by the server, set the credentials.
   request->Credentials = CredentialCache::DefaultCredentials;
   
   // Get the response.
   HttpWebResponse^ response = dynamic_cast<HttpWebResponse^>(request->GetResponse());
   
   // Display the status.
   Console::WriteLine( response->StatusDescription );
   
   // Get the stream containing content returned by the server.
   Stream^ dataStream = response->GetResponseStream();
   
   // Open the stream using a StreamReader for easy access.
   StreamReader^ reader = gcnew StreamReader( dataStream );
   
   // Read the content.
   String^ responseFromServer = reader->ReadToEnd();
   
   // Display the content.
   Console::WriteLine( responseFromServer );
   
   // Cleanup the streams and the response.
   reader->Close();
   dataStream->Close();
   response->Close();
}
using System;
using System.IO;
using System.Net;
using System.Text;

namespace Examples.System.Net
{
    public class WebRequestGetExample
    {
        public static void Main ()
        {
            // Create a request for the URL. 		
            WebRequest request = WebRequest.Create ("http://www.contoso.com/default.html");
            // If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials;
            // Get the response.
            HttpWebResponse response = (HttpWebResponse)request.GetResponse ();
            // Display the status.
            Console.WriteLine (response.StatusDescription);
            // Get the stream containing content returned by the server.
            Stream dataStream = response.GetResponseStream ();
            // Open the stream using a StreamReader for easy access.
            StreamReader reader = new StreamReader (dataStream);
            // Read the content.
            string responseFromServer = reader.ReadToEnd ();
            // Display the content.
            Console.WriteLine (responseFromServer);
            // Cleanup the streams and the response.
            reader.Close ();
            dataStream.Close ();
            response.Close ();
        }
    }
}
Imports System.IO
Imports System.Net
Imports System.Text

Namespace Examples.System.Net
    Public Class WebRequestGetExample

        Public Shared Sub Main()
            ' Create a request for the URL. 		
            Dim request As WebRequest = WebRequest.Create("http://www.contoso.com/default.html")
            ' If required by the server, set the credentials.
            request.Credentials = CredentialCache.DefaultCredentials
            ' Get the response.
            Dim response As HttpWebResponse = CType(request.GetResponse(), HttpWebResponse)
            ' Display the status.
            Console.WriteLine(response.StatusDescription)
            ' Get the stream containing content returned by the server.
            Dim dataStream As Stream = response.GetResponseStream()
            ' Open the stream using a StreamReader for easy access.
            Dim reader As New StreamReader(dataStream)
            ' Read the content.
            Dim responseFromServer As String = reader.ReadToEnd()
            ' Display the content.
            Console.WriteLine(responseFromServer)
            ' Cleanup the streams and the response.
            reader.Close()
            dataStream.Close()
            response.Close()
        End Sub
    End Class
End Namespace

備註

重要

請勿使用 WebRequest 或其衍生類別進行新的開發。 請改用 System.Net.Http.HttpClient 類別。

WebRequestabstract是的基類。NET 的要求/回應模型,用於從因特網存取數據。 使用要求/回應模型的應用程式可以透過通訊協議無關的方式從因特網要求數據,而應用程式會與 類別的 WebRequest 實例搭配使用,而通訊協定特定子代類別則會執行要求的詳細數據。

要求會從應用程式傳送至特定的 URI,例如伺服器上的網頁。 URI 會決定適當的子系類別,以從為應用程式註冊的 WebRequest 子系列表建立。 WebRequest 子代通常會註冊來處理特定通訊協定,例如 HTTP 或 FTP,但可以註冊來處理伺服器上特定伺服器或路徑的要求。

類別 WebRequest 會在存取因特網資源時發生錯誤時擲回 WebException 。 屬性 Status 是指出錯誤來源的其中 WebExceptionStatus 一個值。 當 為 WebExceptionStatus.ProtocolErrorStatus,屬性ResponseWebResponse包含從網際網路資源接收的 。

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

注意

Create使用方法來初始化新的WebRequest實例。 請勿使用 建 WebRequest 構函式。

注意

如果建立 WebRequest 物件的應用程式會以 Normal 使用者的認證執行,除非明確授與使用者許可權給使用者,否則應用程式將無法存取安裝在本機電腦存放區中的憑證。

給實施者的注意事項

當您繼承自 WebRequest時,必須覆寫下列成員:Method、、 RequestUriCredentialsGetRequestStream()ContentTypeHeadersPreAuthenticateContentLengthGetResponse()BeginGetRequestStream(AsyncCallback, Object)EndGetRequestStream(IAsyncResult)BeginGetResponse(AsyncCallback, Object)和 。EndGetResponse(IAsyncResult) 此外,您必須提供 介面的實作 IWebRequestCreate ,這個實作會 Create(Uri) 定義呼叫 Create(Uri)時所使用的方法。 您必須使用 RegisterPrefix(String, IWebRequestCreate) 方法或組態檔來註冊實作 IWebRequestCreate 介面的類別。

建構函式

WebRequest()
已淘汰.

初始化 WebRequest 類別的新執行個體。

WebRequest(SerializationInfo, StreamingContext)
已淘汰.

初始化 WebRequest 類別的新執行個體,這個執行個體是來自 SerializationInfoStreamingContext 類別的指定執行個體。

屬性

AuthenticationLevel

取得或設定值,指出用於這個要求的驗證和模擬等級。

CachePolicy

取得或設定這個要求的快取原則。

ConnectionGroupName

在子代類別中覆寫時,取得或設定要求的連接群組名稱。

ContentLength

在子代類別中覆寫時,取得或設定正在傳送要求資料的內容長度。

ContentType

在子代類別中覆寫時,取得或設定正在傳送要求資料的內容類型。

CreatorInstance
已淘汰.

在子代類別中覆寫時,取得衍生自 IWebRequestCreate 類別的 Factory 物件,用來建立執行個體化的 WebRequest 以對指定的 URI 提出要求。

Credentials

在子代類別中覆寫時,取得或設定使用網際網路資源驗證要求的網路認證。

DefaultCachePolicy

取得或設定這個要求的預設快取原則。

DefaultWebProxy

取得或設定全域 HTTP Proxy。

Headers

在子代類別中覆寫時,取得或設定與要求相關聯的標頭名稱/值組集合。

ImpersonationLevel

取得或設定目前要求的模擬等級。

Method

在子代類別中覆寫時,取得或設定這個要求中要使用的通訊協定方法。

PreAuthenticate

在子代類別中覆寫時,指出是否要預先驗證要求。

Proxy

在子代類別中覆寫時,取得或設定要用來存取這個網際網路資源的網路 Proxy。

RequestUri

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

Timeout

取得或設定要求逾時之前的時間長度 (以毫秒為單位)。

UseDefaultCredentials

在子代類別中覆寫時,取得或設定 Boolean 值,控制 DefaultCredentials 是否隨著要求傳送。

方法

Abort()

中止要求。

BeginGetRequestStream(AsyncCallback, Object)

在子代類別中覆寫時,會提供 GetRequestStream() 方法的非同步版本。

BeginGetResponse(AsyncCallback, Object)

在子代類別中覆寫時,開始網際網路資源的非同步要求。

Create(String)
已淘汰.

為指定的 URI 配置,初始化新的 WebRequest 執行個體。

Create(Uri)
已淘汰.

為指定的 URI 配置,初始化新的 WebRequest 執行個體。

CreateDefault(Uri)
已淘汰.

為指定的 URI 配置,初始化新的 WebRequest 執行個體。

CreateHttp(String)
已淘汰.

為指定的 URI 字串,初始化新的 HttpWebRequest 執行個體。

CreateHttp(Uri)
已淘汰.

為指定的 URI 配置,初始化新的 HttpWebRequest 執行個體。

CreateObjRef(Type)

建立包含所有相關資訊的物件,這些資訊是產生用來與遠端物件通訊的所需 Proxy。

(繼承來源 MarshalByRefObject)
EndGetRequestStream(IAsyncResult)

在子代類別中覆寫時,傳回 Stream,以便將資料寫入至網際網路資源。

EndGetResponse(IAsyncResult)

在子代類別中覆寫時,傳回 WebResponse

Equals(Object)

判斷指定的物件是否等於目前的物件。

(繼承來源 Object)
GetHashCode()

做為預設雜湊函式。

(繼承來源 Object)
GetLifetimeService()
已淘汰.

擷取控制這個執行個體存留期 (Lifetime) 原則的目前存留期服務物件。

(繼承來源 MarshalByRefObject)
GetObjectData(SerializationInfo, StreamingContext)
已淘汰.

將序列化目標物件所需的資料填入 SerializationInfo

GetRequestStream()

在子代類別中覆寫時,傳回 Stream,以便將資料寫入至網際網路資源。

GetRequestStreamAsync()

在子代類別中覆寫時,傳回以非同步作業方式將資料寫入網際網路資源的 Stream

GetResponse()

在子代類別中覆寫時,傳回對網際網路要求的回應。

GetResponseAsync()

在子代類別中覆寫時,傳回對網際網路要求的回應,做為非同步作業。

GetSystemWebProxy()

傳回 Proxy,其組態為目前模擬使用者的 Internet Explorer 設定。

GetType()

取得目前執行個體的 Type

(繼承來源 Object)
InitializeLifetimeService()
已淘汰.

取得存留期服務物件,以控制這個執行個體的存留期原則。

(繼承來源 MarshalByRefObject)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
MemberwiseClone(Boolean)

建立目前 MarshalByRefObject 物件的淺層複本。

(繼承來源 MarshalByRefObject)
RegisterPortableWebRequestCreator(IWebRequestCreate)
已淘汰.

註冊 IWebRequestCreate 物件。

RegisterPrefix(String, IWebRequestCreate)

註冊指定 URI 的 WebRequest 子代。

ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

ISerializable.GetObjectData(SerializationInfo, StreamingContext)
已淘汰.

在子代類別中覆寫時,以序列化 WebRequest 所需的資料填入 SerializationInfo 執行個體。

適用於

另請參閱