WebRequest 類別
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
對統一資源識別元 (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 。
WebRequest 是 的 abstract
基類。從網際網路存取資料的 NET 要求/回應模型。 使用要求/回應模型的應用程式可以透過通訊協定無關的方式向網際網路要求資料,讓應用程式能與 類別的 WebRequest 實例搭配使用,而通訊協定特定子系類別則會執行要求的詳細資料。
要求會從應用程式傳送至特定的 URI,例如伺服器上的網頁。 URI 會決定從為應用程式註冊的 WebRequest 子系列表建立的適當子系類別。 WebRequest 子系通常會註冊來處理特定通訊協定,例如 HTTP 或 FTP,但可以註冊來處理對伺服器上特定伺服器或路徑的要求。
類別 WebRequest 會在 WebException 存取網際網路資源時發生錯誤時擲回 。 屬性 Status 是指出錯誤來源的 WebExceptionStatus 其中一個值。 當 為 WebExceptionStatus.ProtocolError 時 Status , Response 屬性會 WebResponse 包含從網際網路資源接收的 。
WebRequest因為類別是 abstract
類別,所以執行時間實例的實際行為是由 方法所 Create 傳回的 WebRequest 子系類別所決定。 如需預設值和例外狀況的詳細資訊,請參閱 子系類別的檔,例如 HttpWebRequest 和 FileWebRequest 。
注意
Create使用 方法來初始化新的 WebRequest 實例。 請勿使用 建構函 WebRequest 式。
注意
如果建立 WebRequest 物件的應用程式會以 Normal 使用者的認證執行,除非已明確將許可權授與使用者,否則應用程式將無法存取本機電腦存放區中安裝的憑證。
給實施者的注意事項
當您繼承自 WebRequest 時,必須覆寫下列成員: Method 、 RequestUri 、 ContentTypeContentLengthPreAuthenticateGetRequestStream()HeadersCredentialsBeginGetRequestStream(AsyncCallback, Object) 、、 EndGetRequestStream(IAsyncResult) 、 GetResponse()BeginGetResponse(AsyncCallback, Object) 、 和 。 EndGetResponse(IAsyncResult) 此外,您必須提供 介面的實作 IWebRequestCreate ,以定義 Create(Uri) 呼叫 Create(Uri) 時所使用的方法。 您必須使用 RegisterPrefix(String, IWebRequestCreate) 方法或組態檔來註冊實作 IWebRequestCreate 介面的類別。
建構函式
WebRequest() |
已過時。
初始化 WebRequest 類別的新執行個體。 |
WebRequest(SerializationInfo, StreamingContext) |
已過時。
初始化 WebRequest 類別的新執行個體,這個執行個體是來自 SerializationInfo 和 StreamingContext 類別的指定執行個體。 |
屬性
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 是否隨著要求傳送。 |
方法
明確介面實作
ISerializable.GetObjectData(SerializationInfo, StreamingContext) |
已過時。
在子代類別中覆寫時,以序列化 WebRequest 所需的資料填入 SerializationInfo 執行個體。 |